Skip to content

Instantly share code, notes, and snippets.

@SparK-Cruz
Last active June 11, 2018 17:13
Show Gist options
  • Save SparK-Cruz/bf8d460467d108215bd3a9d0653335ff to your computer and use it in GitHub Desktop.
Save SparK-Cruz/bf8d460467d108215bd3a9d0653335ff to your computer and use it in GitHub Desktop.
Accessible attributes trait for mass assignment in php
<?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