Skip to content

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