Created
March 18, 2020 20:51
-
-
Save wlkns/c7d60a2d0947760c329880922e548d3b to your computer and use it in GitHub Desktop.
Generate $fillable property of Model
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 | |
$entities = glob("app/Entities/**/*.php"); | |
foreach ($entities as $entity) { | |
$className = str_replace('/', '\\', ucfirst(substr($entity, 0, -4))); | |
$class = new $className; | |
dump($class->getTable()); | |
$columns = collect(DB::select("SHOW COLUMNS FROM `{$class->getTable()}`"))->filter(function ($column) { | |
return !in_array($column->Field, ['id', 'created_at', 'updated_at', 'deleted_at']); | |
}); | |
$fields = str_replace(["\n", '"'], ["\n ", "'"], json_encode($columns->pluck('Field'), JSON_PRETTY_PRINT)); | |
$contents = file_get_contents($entity); | |
file_put_contents($entity, preg_replace('#\$(guarded|fillable)\s+\=\s+\[[^\]]+]\;#isU', sprintf('$fillable = %s;', $fields), $contents)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment