Created
October 12, 2015 16:08
-
-
Save rmrhz/7c2802e52e5a79af8c92 to your computer and use it in GitHub Desktop.
An impractical use for mass assignments in Laravel.
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 | |
class Repository | |
{ | |
/** | |
* Mass assignment based on fillable, hidden, and guarded attributes | |
* | |
* @param obj $model | |
* @param arr $insertArray | |
* @return obj | |
*/ | |
protected function mapInserts($model, $insertArray) | |
{ | |
$fields = array_unique( | |
array_merge($model->getFillable(), $model->getHidden(), $model->getGuarded()) | |
); | |
foreach($insertArray as $key => $val) | |
{ | |
if ( in_array($key, $fields) ) { | |
$model->{$key} = $val; | |
} | |
} | |
return $model; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage