Work with Laravel Automatic Migrations, including model creation, schema management, and migration execution.
Use this skill when working with Laravel Automatic Migrations to create models with built-in schema definitions, run automatic migrations, or manage database schemas through model-based migrations.
Laravel Automatic Migrations allows you to define database schema directly in your Eloquent models using a static migration method, eliminating the need for separate migration files for most use cases.
Create a new Eloquent model with automatic migration support.
Usage:
php artisan make:amodel {model_name} {options}
Options:
--force: Overwrite existing model--no-factory: Skip factory creation--no-soft-delete: Skip soft delete trait and column--no-static-migration: Skip migration method generationExample:
php artisan make:amodel BlogPost
Execute automatic migrations based on model definitions.
Usage:
php artisan migrate:auto {options}
Options:
--fresh: Wipe database before migrating--seed: Run database seeders after migration--force: Run migrations in production environment--info: Display additional informationExample:
php artisan migrate:auto --fresh --seed
Discover and list all models that support automatic migrations.
Usage:
php artisan migrate:auto-discover
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
class Product extends Model
{
public static function migration(Blueprint $table)
{
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->decimal('price', 8, 2);
$table->boolean('active')->default(true);
$table->timestamps();
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Category extends Model
{
use HasFactory, SoftDeletes;
public $migrationOrder = 1; // Run first
public static function migration(Blueprint $table)
{
$table->id();
$table->string('name');
$table->string('slug')->unique();
$table->text('description')->nullable();
$table->timestamps();
$table->softDeletes();
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Schema\Blueprint;
use Faker\Generator as Faker;
class Post extends Model
{
use HasFactory;
public static function migration(Blueprint $table)
{
$table->id();
$table->string('title');
$table->string('slug')->unique();
$table->text('content');
$table->foreignId('user_id')->constrained();
$table->foreignId('category_id')->constrained();
$table->timestamps();
}
public function definition(Faker $faker): array
{
return [
'title' => $faker->sentence,
'slug' => $faker->slug,
'content' => $faker->paragraphs(3, true),
'user_id' => User::factory(),
'category_id' => Category::factory(),
'created_at' => $faker->dateTimeThisMonth(),
];
}
}
Use the $migrationOrder property to control the order of table creation, especially important for foreign key constraints:
class User extends Model
{
public $migrationOrder = 1; // First
public static function migration(Blueprint $table)
{
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
}
}
class Post extends Model
{
public $migrationOrder = 2; // Second (after User)
public static function migration(Blueprint $table)
{
$table->id();
$table->string('title');
$table->foreignId('user_id')->constrained(); // References users table
$table->timestamps();
}
}
BlogPost becomes blog_posts)$migrationOrder for models with foreign key relationshipstimestamps() in your migration methodsmigrate:auto-discover to verify that your models are being detectedThe package works seamlessly with traditional Laravel migrations:
public static function migration(Blueprint $table)
{
$table->id();
$table->string('name');
$table->timestamps();
$table->softDeletes(); // Add this for soft delete support
}
public static function migration(Blueprint $table)
{
$table->id();
$table->string('title');
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
}
public static function migration(Blueprint $table)
{
$table->id();
$table->string('email')->unique();
$table->string('name');
$table->index('name'); // Additional index
$table->timestamps();
}
$migrationOrderUse migrate:auto-discover to list all discoverable models and verify your models are being detected.
Use verbose output with migrations for detailed information:
php artisan migrate:auto --verbose
```3d:["$","$L44",null,{"content":"$45","frontMatter":{"name":"auto-migrate-development","description":"Work with Laravel Automatic Migrations, including model creation, schema management, and migration execution."}}]