Framework: Eloquent where('column') and whereColumn() without value should behave like boolean

Created on 31 Oct 2018  ·  11Comments  ·  Source: laravel/framework

Currently, skipping value for where('column') will behave as IS NULL, like this:

$games = Game::where('is_active')->get();

This will make a query:

select * from `games` where `is_active` is null

If we try with magic where:

$games = Game::whereIsActive()->get();

It will produce an error:

Undefined offset: 0

I think it would be much more intuitive, if it behave as boolean, running the following query:

select * from `games` where `is_active`

I understand what was the thinking pattern behind making null default, but reading line Game::where('is_active') really doesn't make it obvious that we are actually looking for a null.

Most helpful comment

@driesvints could we have this relabelled as an improvement? This is not a bug – there is no documented behaviour for not passing an argument, and it will result in a breaking change. I agree that it syntactically makes more sense, but it is not a bug.

All 11 comments

Try to tape this code:

$games = Game::where('is_active')->toSql();
$games = Game::whereIsActive()->toSql();

and compare it to view difference.

@hichxm what's your point?

I really don't think not passing a value makes much sense even if the default behavior of the current where method allows this. Since it doesn't influences the query at all by just doing a where on a column without a check I don't consider this as an actual bug.

It does make a lot of sense for boolean values, which are also supported by SQL (WHERE without any operator).

@powelski you'd still need to pass a value in this case?

Closing upvoted ticket with improvement suggestion, because one person thinks „it’s not a bug”? Wow.

@driesvints no, you would not need to pass a value. Just like you can do it in a query:

SELECT * FROM people WHERE is_alive

It would feel natural to express this way, especially that it's on par with PHP's non-empty truthfulness.

Alright, wel feel free to send in a PR and see if it gets accepted.

@driesvints could we have this relabelled as an improvement? This is not a bug – there is no documented behaviour for not passing an argument, and it will result in a breaking change. I agree that it syntactically makes more sense, but it is not a bug.

Yup, I agree. Relabelled.

Closing since the PR was rejected.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

digirew picture digirew  ·  3Comments

YannPl picture YannPl  ·  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  ·  3Comments

Fuzzyma picture Fuzzyma  ·  3Comments

PhiloNL picture PhiloNL  ·  3Comments