Automatically sync Laravel models from migration files, including fillable, casts, and relationships.
khairy/migration-model-sync is a Laravel package for automatically sync laravel models from migration files, including fillable, casts, and relationships..
It currently has 2 GitHub stars and 35 downloads on Packagist (latest version v1.0.0).
Install it with composer require khairy/migration-model-sync.
Discover more Laravel packages by khairy
or browse all Laravel packages to compare alternatives.
Last updated
Laravel Migration Model Sync is a development tool that automatically generates or synchronizes Eloquent model files based on your migration files. It extracts columns, data types, and foreign key relationships and applies them directly to your model"s $fillable, $casts, and relationship methods.
$fillable, $casts, and relationshipsbelongsTo from foreign keyshasMany, hasOne, and belongsToManyconfig/modelsync.phpmodel:sync-all command to synchronize all models from available migration files.model:generate-migration User β creates a migration file for a specific modelmodel:generate-migrations β auto-generates all migration files from models (with dependency-aware sorting)Laravel versions supported: 8.x β 12.x
composer require khairy/migration-model-sync
php artisan vendor:publish --tag=modelsync-config
This creates the config file: config/modelsync.php
Edit config/modelsync.php to customize behavior:
return [
\"model_path\" => app_path(\"Models\"),
\"ignore_columns\" => [
\"created_at\",
\"updated_at\",
],
\"relationship_overrides\" => [
// \"created_by\" => \"belongsTo:App\\Models\\User\"
],
];
app/Models.$fillable and $casts properties of the generated models. Common examples include created_at and updated_at.php artisan model:sync User
This command will:
users table$fillable, $casts, and any belongsTo relationships to app/Models/User.phpphp artisan model:sync-all
This command will:
$fillable, $casts, and inferred belongsTo relationships.php artisan model:generate-migration User
This command will:
app/Models/User.php$fillable, $casts, and any belongsTo() relationships(string, timestamp, json, etc.)create_users_table.php migration in database/migrationsforeignId()->constrained()->on(...) if belongsTo() is presenttimestamps and soft deletes if applicablephp artisan model:generate-migrations
This command will:
$fillable, $casts, and belongsTo() methodscreate_*_table.php migration files--only=User,Post , --except=Log , --sort=none , and --force for advanced control.Schema::create(\"profiles\", function (Blueprint $table) {
$table->id();
$table->string(\"name\");
$table->string(\"email\");
$table->timestamp(\"email_verified_at\")->nullable();
$table->string(\"password\");
$table->foreignId(\"user_id\")->constrained();
$table->timestamps();
});
namespace App\\Models;
use Illuminate\\Database\\Eloquent\\Model;
class Profile extends Model
{
protected $fillable = [\"name\", \"email\", \"email_verified_at\", \"password\", \"user_id\"];
protected $casts = [
\"email_verified_at\" => \"datetime\",
\"created_at\" => \"datetime\",
\"updated_at\" => \"datetime\",
];
public function user()
{
return $this->belongsTo(User::class);
}
}
| Relation Type | Detection Logic |
|----------------|----------------------------------------------------------------------------------|
| belongsTo | foreignId(\"user_id\")->constrained() or foreign key chains |
| hasMany | Inferred from other tables containing a foreign key to this model (todo) |
| hasOne | Inferred from profiles, settings, etc. (todo) |
| belongsToMany | Detected from pivot tables like role_user (todo) |
You can override default behavior:
relationship_overrides in config to manually define model relationsPull requests are welcome! Please fork the repo, improve, and submit a PR. For major changes, open an issue first.
MIT License Β© Khairy