Skip to content

Instantly share code, notes, and snippets.

@wlkns
Created March 18, 2020 20:51
Show Gist options
  • Save wlkns/c7d60a2d0947760c329880922e548d3b to your computer and use it in GitHub Desktop.
Save wlkns/c7d60a2d0947760c329880922e548d3b to your computer and use it in GitHub Desktop.
Generate $fillable property of Model
<?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