SyncModelFillable is a Laravel package designed to help automatically sync a model's $fillable
fields with its database migration columns. 🎉 With just a simple Artisan command, you can keep your model properties up-to-date with your migration files effortlessly.
- 🛠️ Syncs model
$fillable
properties with migration columns. - 📦 Supports Laravel versions 8, 9, 10, and 11.
- ⚙️ Customizable to exclude specific columns, like timestamps.
-
Install the package via Composer:
composer require muzammal/syncmodelfillable
-
(Optional) Publish the configuration file:
If you'd like to customize which columns are excluded from the
$fillable
fields, publish the configuration file:php artisan vendor:publish --provider="Muzammal\Syncmodelfillable\SyncModelFillableServiceProvider"
This will create a
config/syncfillable.php
file where you can specify columns to exclude (such ascreated_at
,updated_at
,deleted_at
etc.).
This package provides an Artisan command sync:fillable
that lets you sync a model's $fillable
fields with its migration columns.
To sync the $fillable
fields of a specific model, use the command with the model name. For example, if you have a model named Post
:
php artisan sync:fillable Post
This will:
- Look for the
Post
model in theapp/Models
directory. - Find the migration file associated with the model’s database table.
- Update the
$fillable
property in the model with the columns from the migration file.
To sync all models in the app/Models
directory, use all
as the parameter:
php artisan sync:fillable all
This will:
- Look for all models in the
app/Models
directory. - Match each model with its migration file.
- Update the
$fillable
property for each model.
The configuration file syncfillable.php
allows you to specify which columns to exclude from the $fillable
fields. By default, common timestamp columns (created_at
, updated_at
, deleted_at
) are excluded.
Example configuration:
return [
'excluded_columns' => ['created_at', 'updated_at', 'deleted_at'],
];
Add any column names here that you want to exclude from the $fillable
fields.
Suppose you have a Post
model with a migration that defines columns such as name
, slug
, and content
. Running the following command:
php artisan sync:fillable Post
Would automatically set the $fillable
fields in Post.php
as follows:
protected $fillable = ['name', 'slug', 'content'];
This package is open-source software licensed under the MIT license.