2016-06-24 19:21
浏览 537

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.

图片转代码服务由CSDN问答提供 功能建议

通过将以下代码片段添加到Eloquent模型中,我可以强制模型在所有数据库调用中包含某些SQL约束 使用该模型制作:

  public function newQuery($ excludeDeleted = true)
 //父Eloquent类的“newQuery”方法的所有功能; 并为该实例添加约束。
 $ sql = parent :: newQuery($ excludeDeleted); 
 $ sql-> where('example','test'); 
 \  n返回$ sql; 

但是,在例如添加约束时 在上面的 WHERE`example` ='test 约束之后,总是会添加那些附加约束。

是否可以强制执行某个约束 还增加了其他约束? 而不是BEFORE? 我需要遵守现有数据库索引的顺序。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题