dongzinen1061 2016-06-24 19:21
浏览 643
已采纳

Laravel / Eloquent - 查询中newQuery约束的位置

By adding the following code snippet to an Eloquent Model I can force the Model to include certain SQL constraints on all database calls ever being made using that Model:

public function newQuery($excludeDeleted = true)
{
    // Make use of all functionalities of the "newQuery" method of the
    // parent Eloquent class; and add constraints to that instance.
    $sql = parent::newQuery($excludeDeleted);

    // Example constraint
    $sql->where('example', 'test');

    return $sql;
}

However, when adding constraints in e.g. the controller, those additional constraints will always be added AFTER the above WHERE `example` = 'test constraint.

Is it possible to force a certain constraint AFTER all other constraints are added? Instead of BEFORE? I have this need to comply with the order of existing database indexes.

  • 写回答

1条回答 默认 最新

  • dqq3623 2016-06-25 01:57
    关注

    First, you should be using global scopes to implement this functionality, not overriding the newQuery method.

    Second, if your only concern is index usage, the position of columns in the where clause does not matter for index selection. The order of the columns in the index definition matters, but not the order in the where clause.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失