Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Adds the valueOfFail() Eloquent builder method #38707

Merged
merged 1 commit into from
Sep 8, 2021
Merged

[8.x] Adds the valueOfFail() Eloquent builder method #38707

merged 1 commit into from
Sep 8, 2021

Conversation

DarkGhostHunter
Copy link
Contributor

@DarkGhostHunter DarkGhostHunter commented Sep 7, 2021

What?

It's the same value() but it will fail miserably if the model is not found.

// Before:
$votes = User::where('name', 'John')->firstOrFail('votes')->votes;

// Now:
$votes = User::where('name', 'John')->valueOrFail('votes');

This is miles better if model contains properties that can be null.

How?

It just copy-pastes the value() method but instead uses firstOrFail() and removes the condition since the model is expected to exist.

BC?

None, it's additive.

Notes

  • I decided to not include valueOr() since you can use ?? or ??=, which are awesome.

@taylorotwell taylorotwell merged commit d73de59 into laravel:8.x Sep 8, 2021
victorvilella pushed a commit to cdsistemas/framework that referenced this pull request Oct 12, 2021
@DarkGhostHunter DarkGhostHunter deleted the feat/value-or-fail-2 branch November 25, 2021 04:04
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.

2 participants