Skip to content

[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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 254 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasConditions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
<?php

namespace Illuminate\Database\Eloquent\Concerns;

trait HasConditions
{
/**
* Fill the model with an array of attributes if the condition is truthy.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param (\Closure($this):array)|array $attributes
* @return $this
*/
public function fillWhen($condition, $attributes = [])
{
return value($condition, $this) ? $this->fill(value($attributes, $this)) : $this;
}

/**
* Fill the model with an array of attributes if the condition is falsy.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param (\Closure($this):array)|array $attributes
* @return $this
*/
public function fillUnless($condition, $attributes = [])
{
return ! value($condition, $this) ? $this->fill(value($attributes, $this)) : $this;
}

/**
* Fill the model with an array of attributes if the condition is truthy. Force mass assignment.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param (\Closure($this):array)|array $attributes
* @return $this
*/
public function forceFillWhen($condition, $attributes = [])
{
return value($condition, $this) ? $this->forceFill(value($attributes, $this)) : $this;
}

/**
* Fill the model with an array of attributes if the condition is falsy. Force mass assignment.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param (\Closure($this):array)|array $attributes
* @return $this
*/
public function forceFillUnless($condition, $attributes = [])
{
return ! value($condition, $this) ? $this->forceFill(value($attributes, $this)) : $this;
}

/**
* Save the model to the database if the condition is true.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param array $options
* @param bool $quietly
* @return bool
*/
public function saveWhen($condition, array $options = [], $quietly = false)
{
if (value($condition, $this)) {
return $quietly ? $this->saveQuietly($options) : $this->save($options);
}

return false;
}

/**
* Save the model to the database if the condition is true.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param array $options
* @param bool $quietly
* @return bool
*/
public function saveUnless($condition, array $options = [], $quietly = false)
{
if (! value($condition, $this)) {
return $quietly ? $this->saveQuietly($options) : $this->save($options);
}

return false;
}

/**
* Save the model to the database if the condition is true.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param array $options
* @return bool
*/
public function saveQuietlyWhen($condition, array $options = [])
{
return $this->saveWhen($condition, $options, true);
}

/**
* Save the model to the database if the condition is true.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param array $options
* @return bool
*/
public function saveQuietlyUnless($condition, array $options = [])
{
return $this->saveUnless($condition, $options, true);
}

/**
* Save the model to the database if the condition is true.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param (\Closure($this):array)|array $attributes
* @param array $options
* @param bool $quietly
* @return bool
*/
public function updateWhen($condition, $attributes = [], array $options = [], $quietly = false)
{
if (value($condition, $this)) {
return $quietly
? $this->updateQuietly(value($attributes, $this), $options)
: $this->update(value($attributes, $this), $options);
}

return false;
}

/**
* Save the model to the database if the condition is true.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param (\Closure($this):array)|array $attributes
* @param array $options
* @param bool $quietly
* @return bool
*/
public function updateUnless($condition, $attributes = [], array $options = [], $quietly = false)
{
if (! value($condition, $this)) {
return $quietly
? $this->updateQuietly(value($attributes, $this), $options)
: $this->update(value($attributes, $this), $options);
}

return false;
}

/**
* Save the model to the database if the condition is true, without raising any events.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param (\Closure($this):array)|array $attributes
* @param array $options
* @return bool
*/
public function updateQuietlyWhen($condition, $attributes = [], array $options = [])
{
return $this->updateWhen($condition, $attributes, $options, true);
}

/**
* Save the model to the database if the condition is true, without raising any events.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param (\Closure($this):array)|array $attributes
* @param array $options
* @return bool
*/
public function updateQuietlyUnless($condition, $attributes = [], array $options = [])
{
return $this->updateUnless($condition, $attributes, $options, true);
}

/**
* Delete the model to the database if the condition is truthy.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param bool $quietly
* @return bool
*/
public function deleteWhen($condition, $quietly = false)
{
if (value($condition, $this)) {
return $quietly ? $this->deleteQuietly() : $this->delete();
}

return false;
}

/**
* Delete the model to the database if the condition is falsy.
*
* @param (\Closure($this):mixed)|mixed $condition
* @param bool $quietly
* @return bool
*/
public function deleteUnless($condition, $quietly = false)
{
if (! value($condition, $this)) {
return $quietly ? $this->deleteQuietly() : $this->delete();
}

return false;
}

/**
* Delete the model to the database if the condition is truthy, without raising any events.
*
* @param (\Closure($this):mixed)|mixed $condition
* @return bool
*/
public function deleteQuietlyWhen($condition)
{
return $this->deleteWhen($condition, true);
}

/**
* Delete the model to the database if the condition is falsy, without raising any events.
*
* @param (\Closure($this):mixed)|mixed $condition
* @return bool
*/
public function deleteQuietlyUnless($condition)
{
return $this->deleteUnless($condition, true);
}

/**
* Force delete the model from the database if the condition is truthy.
*
* @param (\Closure($this):mixed)|mixed $condition
* @return bool
*/
public function forceDeleteWhen($condition)
{
return value($condition, $this) ? $this->forceDelete() : false;
}

/**
* Force delete the model from the database if the condition is falsy.
*
* @param (\Closure($this):mixed)|mixed $condition
* @return bool
*/
public function forceDeleteUnless($condition)
{
return ! value($condition, $this) ? $this->forceDelete() : false;
}
}
1 change: 1 addition & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
Concerns\GuardsAttributes,
Concerns\PreventsCircularRecursion,
Concerns\TransformsToResource,
Concerns\HasConditions,
ForwardsCalls;
/** @use HasCollection<\Illuminate\Database\Eloquent\Collection<array-key, static & self>> */
use HasCollection;
Expand Down
Loading