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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题