Created
March 14, 2017 16:25
-
-
Save JordanDalton/f952b053ef188e8750177bf0260ce166 to your computer and use it in GitHub Desktop.
Here's how you could use a trait to update the fillable property on eloquent models.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
trait Auditable | |
{ | |
/** | |
* The fillable properties for this trait which will be applied to the model. | |
* | |
* @var array | |
*/ | |
protected $auditable_fillable = [ | |
'audited', | |
'auditor_agrees', | |
]; | |
/** | |
* Mark the record as audited. | |
* | |
* @param $auditor_agrees | |
* | |
* @return bool | |
*/ | |
public function markAudited($auditor_agrees) | |
{ | |
return $this->update(['audited' => true, 'auditor_agrees' => $auditor_agrees]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$vehicle = App\Vehicle::find(1); | |
$fillables = $vehicle->getFillable(); // ['plate', 'audited', 'auditor_agrees'] | |
$vehicle->markAudited(true); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use ReflectionObject; | |
class Model extends \Illuminate\Database\Eloquent\Model | |
{ | |
/** | |
* Get the fillable attributes for the model. | |
* | |
* @return array | |
*/ | |
public function getFillable() | |
{ | |
return array_merge(parent::getFillable(), $this->getTraitFillables()); | |
} | |
/** | |
* Obtain all the fillable property value from traits. | |
* | |
* @return \Illuminate\Support\Collection | |
*/ | |
public function getTraitFillables() | |
{ | |
$reflect = (new ReflectionObject($this)); | |
$properties = collect($reflect->getProperties())->filter(function($property){ return str_contains($property->getName(), '_fillable'); }); | |
$fillables = collect(); | |
$properties->each(function($property) use($fillables){ | |
$property->setAccessible(true); | |
foreach($property->getValue($this) as $value) | |
{ | |
$fillables->push($value); | |
} | |
}); | |
return $fillables->unique()->toArray(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use App\Models\Model; | |
use App\Traits\Auditable; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
class Vehicle extends Model | |
{ | |
use Auditable, | |
SoftDeletes; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'plate' | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment