In my view, the Eloquent ORM where condition resolution scene is not so rich. Many conditions require the introduction of additional ORUSES such as WhereNotin, WhereBetween, and WhereNoTBetween to complete. This is not very friendly when doing some abstract low-level query methods, and the query conditions passed by the upper layer are uncertain. If you can flexibly parse various mixed query conditions (described in the way of array), it will be more efficient and flexible to use.

* @Param Builder $query * @Param $conditions */ public static function renderWhereMixedEloquent(Builder $query, $conditions) { $lastEl = end($conditions); reset($conditions); if (is_string($lastEl) && (('or' == $lastEl || 'and' == $lastEl))) { $logic = $lastEl; array_pop($conditions); } else { $logic = 'and'; } $conditionsKeys = array_keys($conditions); $conditionsKeyFirst = $conditionsKeys[0]; if (is_numeric($conditionsKeyFirst)) { if (is_array($conditions[$conditionsKeyFirst])) { if ('or' == $logic) { $query->where(function (Builder $query) use ($conditions) { foreach ($conditions as $conditionsSub) { $query->orWhere(function (Builder $query) use ($conditionsSub) { static::renderWhereMixedEloquent($query, $conditionsSub); }); }}); } else { $query->where(function (Builder $query) use ($conditions) { foreach ($conditions as $conditionsSub) { $query->where(function (Builder $query) use ($conditionsSub) { static::renderWhereMixedEloquent($query, $conditionsSub); }); }}); } } else { $operator = $conditions[1]; switch ($operator) { case 'in': $query->whereIn($conditions[0], $conditions[2], $logic); break; case 'between': $query->whereBetween($conditions[0], $conditions[2], $logic); break; case 'not in': $query->whereIn($conditions[0], $conditions[2], $logic, true); break; case 'not between': $query->whereBetween($conditions[0], $conditions[2], $logic, true); break; default: $query->where(... $conditions); } } } else { $query->where(function (Builder $query) use ($logic, $conditions) { if ('and' == $logic) { foreach ($conditions as $col => $val) { $query->where([$col => $val]); } } else { foreach ($conditions as $col => $val) { $query->orWhere([$col => $val]); }}}); }}

Use the sample

The simple AND condition

$conditions = [
    'name' => 'lily',
    'sex'   => 'f',
];
$conditions = [
    'name' => 'lily',
    'sex'   => ['f', 'm'],
];

Simple OR condition

$conditions = [
    'name' => 'lily',
    'sex'   => ['f', 'm'],
    'or'
];

Complex AND/OR queries

$conditions = [ [ ['id', '>', 5], ['hobby', 'in', ['football', 'swimming']], ['created_at', 'between', [strtotime('2020-05-20 10:38:41'), strtotime('2021-05-25 10:39:41')]], ], [ ['id', '>', 5], ['hobby', 'in', ['football', 'swimming']], ['created_at', 'between', [strtotime('2020-05-20 10:38:41'), strtotime('2021-05-25 10:39:41')]], ], ]; // group 1 and group 2
$conditions = [ [ ['id', '=', 5], ['hobby', 'in', ['football', 'swimming']], ['created_at', 'between', [strtotime (' 2020-05-20 10:38:41), strtotime (' 2021-05-25 10:39:41)]], 'or' / / group 1 or internal do], [[' id 'and' > ', 5], [' hobby ', 'not in', ['football', 'swimming']], ['created_at', 'not between', [strtotime('2020-05-20 10:38:41'), Strtotime (' 2021-05-25 10:39:41)]],], 'or', / / group 1 or 2];
$conditions = [' sex' => ['f', 'm'], $conditions = [' sex' => ['f', 'm'], $conditions = [' sex' => ['f', 'm'], Can mix, [' name 'and' = ', 'test'], [[' id 'and' > ', 5], [' hobby 'and' in ', [' football ', 'swimming']], [' created_at ', 'between', [strtotime (' 2020-05-20 10:38:41), strtotime (' 2021-05-25 10:39:41)]], 'or' / / group 1 or internal do], [[' id 'and' > ', 5], [' hobby ', 'in', ['football', 'swimming']], ['created_at', 'between', [strtotime('2020-05-20 10:38:41'), Strtotime (' 2021-05-25 10:39:41)]],], 'or', / / group 1 or 2];

Using the instance

$query = User::select("*"); / / is mainly to the Builder object / / $query object reference value in communication User: : renderWhereMixedEloquent ($query, $the conditions); $query->get(); $query = User::query(); $query = User::query(); / / $query object reference value in communication User: : renderWhereMixedEloquent ($query, $the conditions); $query->get();