Last active
June 11, 2018 17:13
-
-
Save SparK-Cruz/bf8d460467d108215bd3a9d0653335ff to your computer and use it in GitHub Desktop.
Accessible attributes trait for mass assignment in php
This file contains hidden or 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 | |
trait Fillable { | |
public function __construct($attributes) { | |
$this->fill($attributes); | |
} | |
public function fill($attributes) : void { | |
foreach((array)$attributes as $key => $value) { | |
if (!property_exists($this, $key)) | |
continue; | |
$this->{$key} = $value; | |
} | |
} | |
public static function many(array $items) : array { | |
return array_map( | |
function($item) { | |
return new static($item); | |
}, | |
$items | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment