-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[12.x] Adds conditionals to Model #55359
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
Conversation
These may need another parameter to make them |
I would prefer |
What will be the difference if we compare this PR with the current use App\Models\User;
$user = User::first();
$user->when(true, fn (User $model) => $model->update(['name' => 'Test'])); |
There is an alternative one-liner approach: if ($condition) $user->update(['value' => computationallyExpensive()]); Or, if you don't like putting "if": $condition && $user->update(['value' => computationallyExpensive()]); Powered by PHP, always has been 👀 |
That's the antichrist and I have a permit to use a M14A.
I'll let this marinate because also other parts of the framework can be similarly approached using that syntax. Update: Also, that returns a boolean and I can't use |
Rather than having dedicated methods for all of these, I feel like we would be better off adding a higher-order $user->when($condition)->update([]);
$user->when($condition)->forceFill([]);
$user->when($condition)->save(); Although, as @osbre mentioned, this is already rather terse in native PHP: $condition && $user->update([]);
$condition && $user->forceFill([]);
$condition && $user->save(); |
Again,
While |
This way, we can get results: |
Most actions modify the same model, there's no point in expecting the value being returned from |
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
Technically, we could differentiate this depending on the first argument passed to the callback (assuming, a callback itself is passed): $user = User::when(true, function (Builder $builder) { // -> proxied to builder
return $builder->where('is_deleted', false);
})->get();
$user->when(true, function (User $user) { // -> executed on model
return $user->is_deleted = false;
}); Another way to differentiate might be whether it's called statically or on the instance. However, both options might have drawbacks and breaking changes. |
What?
Adds conditional fill, save, update, delete and force-delete to Models. This way developers don't have to be confused with the
when
andunless
operations that are proxied to the Eloquent Builder, which are kept as-is and accessible as a static methods from the Model class.The methods are:
forceFillWhen() | forceFillUnless()
: Accepts the condition and fills the attributes usingfill()
method.fillWhen() | fillUnless()
: Accepts the condition and fills the attributes usingforceFill()
method.saveWhen() | saveUnless()
: Accepts the condition and the options to pass to thesave()
method.updateWhen() | updateUnelss()
: Accepts the condition, the attributes, and the options to pass to theupdate()
method.deleteWhen() | deleteUnless()
: Accepts the condition and callsdelete()
.forceDeleteWhen() | forceDeleteUnless()
: Same logic as before but forforceDelete()
.Notice that the conditional methods that accept attributes as an array also accept a callback that returns the array of attributes. This is because a developer may have to execute computationally expensive attributes based on the condition, which can be avoided if the condition fails.
These callbacks receive the model instance, allowing the developer to use a callback from elsewhere, which can make controllers or other methods grow in size.