dongliyan9190 2016-10-25 20:53
浏览 173
已采纳

Laravel Eloquent ORM:构建嵌套的条件查询

I have 5 user titles, defined by booleans: CEO, executive, manager, employee & intern.

I'm building a user search API, and want to turn on/off eloquent queries, returning users with selected titles.

So if I were to search for managers and employees, the query should be

$users = User::where(function($query) 
{
   $query->orWhere('manager')->orWhere('employee');
})
->where([ADDITIONAL CONSTRAINTS... (like age)])->get();

The furthest I have came is:

$query = User::query();

//the respective titles are turned on by 1 and turned off by 0
if($CEO) {
$query = $query->orWhere('CEO');
}

if($executive) {
$query = $query->orWhere('executive');
}

//And so on for each title

In the end the additional where constraints get added like this:

$users = $query->where([Additional constraints])->get();

When searching for managers and employees, the final query would be:

$users = User::orWhere('manager')->orWhere('employee')

->where([ADDITIONAL CONSTRAINTS... (like age)])->get();

The result of this query is that the additional constraints are not always met, because there are orwhere queries before, which allow for unwanted instances to get selected.

I tried replacing the orWhere's with where's, but then users need to check positive for each selected title to get selected. So if I wanted to search for managers and employees, I might get none, because there isn't any user with both titles.

The goal:

  1. I want to add all these conditional 'title-queries' together.
  2. Put them all in one where(function($query) { $query->[all 'title-queries']; }).

Additional comments:

  1. I know that I could also eliminate every other model instead of searching for wanted models. If I would search for managers and employees, I could set where('CEO', '!=', 1) for each unwanted title. I don't want this, because Users with two titles, like employee and interim would get excluded in some cases.

  2. I know that I could write nested conditional queries for each scenario i.e. (manager & ceo, interim & ceo & executive and on ...), but that would take 25 queries and simply is not easily scalable (exponential more queries) if additional user titles are added.

  3. It has to be an Eloquent solution.

  4. Users can have multiple titles.

I have thought hard about this problem, thanks!

  • 写回答

1条回答 默认 最新

  • douya2982 2016-10-25 21:00
    关注

    Maybe you can do something like this ? (with use function keyword)

    <?php
    
    $filters = ['manager', 'employee'];
    
    $users = User::where(function($query) use($filters) {
       foreach( $filters as $filter )
          $query = $query->orWhere($filter);
    })
    ->where([ADDITIONAL CONSTRAINTS... (like age)])->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改