Eltyer Eltyer - 1 month ago 17
PHP Question

Laravel Eloquent ORM: Building nested conditional queries

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!

Answer

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();