1

When using the query builder, if you give eloquent a clause that ends up being an identity, something like

$foo = [];
$query->whereNotIn('column', $foo);

it translates that clause to 1=1 when it builds the query, at least for MySQL. Are there any config options to alter this behavior? I realize it would be ideal to check if $foo is empty before adding it to the query, but I think it would make more sense to just ignore the clause instead of turning it into 1=1 so I would like to know if there is any way to control that.

>>> echo User::whereNotIn('id',[])->toSql()
select * from `users` where 1 = 1 

I'd rather that just evaluate to

select * from `users`
chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • I would not worry about this. Sql engines understand that there is no need to actually do any where checks in a case like this ... the actual query plan will execute just as you desire! – topsail Jun 13 '22 at 21:42
  • 1
    This has been discussed 13 years ago here https://stackoverflow.com/questions/242822/why-would-someone-use-where-1-1-and-conditions-in-a-sql-clause – Manuel Glez Jun 13 '22 at 21:53
  • @ManuelGlez it sounds like the accepted answer of `With the 1=1 at the start, the initial and has something to associate with.` is basically moot since eloquent is smart enough to figure out how to generate a valid query either way. So wth, why is the 1=1 still there? – chiliNUT Jun 13 '22 at 22:00
  • I think that the purpose of 1=1 is to give a simple valid expression that return true, but the really interesting is that `1=1` is accompanied by a **Where** or an **And**. How did eloquent resolve your expression with an empty array like User::whereNotIn('id',[]) ?? – Manuel Glez Jun 13 '22 at 22:05

0 Answers0