Skip to content

[9.x] add findOr method to Query/Builder - #42290

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
mihaileu:query-builder-find-or
May 9, 2022
Merged

taylorotwell merged 2 commits into
laravel:9.xfrom
mihaileu:query-builder-find-or

Conversation

@mihaileu

@mihaileu mihaileu commented May 6, 2022 •

Copy link
Copy Markdown
Contributor

This PR adds a findOr() method to the Query Builder. It is similar with #42092

Example usage:

$data = \DB::table('something')->findOr($id, fn () => throw new CustomException);
$data = \DB::table('something')->findOr($id, fn () => (object) ['id=>'0', 'name'=>'N/A']);

Edit: I updated the example as it was wrong.
Probably there are other usage cases.

@mihaileu mihaileu changed the title add findOr method to Query/Builder [9.x] add findOr method to Query/Builder May 6, 2022
@eduance

eduance commented May 8, 2022 •

Copy link
Copy Markdown
Contributor

Another way of doing this is by using the the ?: operator, but for readability, I'd really like this addition.

@bert-w

bert-w commented May 8, 2022

Copy link
Copy Markdown
Contributor

No support because you can do the same thing with plain PHP:

$id = DB::table('users')->find(9876)?->id ?? throw new Exception('nope');

vs

$id = DB:table('users')->findOr(9876, throw new Exception('nope'));

(also the $callback parameter cannot be null in your implementation)

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

I'm 👎 here, for the reason provided by @bert-w. Since PHP 8.0, throw statements are also expressions.

@davisngl

davisngl commented May 8, 2022 •

Copy link
Copy Markdown

And yet none of you had problems with #42092, when the usage with throwing an exception was highlighted?

@bert-w

bert-w commented May 8, 2022

Copy link
Copy Markdown
Contributor

And yet none of you had problems with #42092, when the usage with throwing an exception was highlighted?

good catch, didnt see that PR since I only browse here occasionally. However that being said I'd take the same stance. Any of the doSomethingOr functions I dont really like since they can often times be achieved with other methods like stated above. So the functions are bloat.

Anyway that means that this function is already merged so RIP

@davisngl

davisngl commented May 8, 2022 •

Copy link
Copy Markdown

And yet none of you had problems with #42092, when the usage with throwing an exception was highlighted?

good catch, didnt see that PR since I only browse here occasionally. However that being said I'd take the same stance. Any of the doSomethingOr functions I dont really like since they can often times be achieved with other methods like stated above. So the functions are bloat.

Anyway that means that this function is already merged so RIP

True.

Would say more, but this isn't the community for being able to take constructive criticism.

@bert-w

bert-w commented May 8, 2022

Copy link
Copy Markdown
Contributor

Apparently there's also #42253 , no clue why these all pop up around the same time. It is kinda weird especially since Laravel 9.x now requires PHP 8+ which adds nice functionality to the language itself. I think it will take a couple of years (yes) for people to start using features frequently like the null safe ($obj->user()?->id) operators...

@taylorotwell
taylorotwell merged commit e02307f into laravel:9.x May 9, 2022
salmanarshad321 pushed a commit to salmanarshad321/framework that referenced this pull request Sep 23, 2026
* add findOr method to Query/Builder

* fixing code style

Co-authored-by: Mihai Leu <mihai.leu@emag.ro>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants