Weblabs Technologies

Streamline Your Laravel Development with In-Model Migrations

Laravel Model Migrations Banner

Weblabs Technologies LTD is excited to announce a powerful addition to our suite of tools for developers using Laravel: the Laravel Model Migrations package. This innovative package offers a streamlined approach to handling database migrations directly within your Laravel models, enhancing the development process and deployment workflow.

What is Laravel Model Migrations?

Laravel Model Migrations is a sophisticated package designed to simplify the management of database structures by allowing migrations to be defined inside Laravel models themselves. Born from the discontinued legodion/lucid project, our package supports Laravel versions from 7 through 11, ensuring compatibility across a wide range of projects.

Key Features

Embedded Migrations and Factories

Traditionally, Laravel migrations are handled through separate migration files. Laravel Model Migrations changes this by allowing migrations and factory definitions to be declared right within your model files. This means you can define the structure of your database tables and the default data for testing all in one place.

Efficient Command Line Integration

Using the lmm:migrate command, developers can apply any changes defined in their model’s migration methods directly to the database using Doctrine DBAL. The package ensures that traditional file-based migrations are executed first, followed by your model method migrations, maintaining a seamless integration with existing Laravel features.

Custom Migration Order

Need to specify the order of migrations? No problem. Laravel Model Migrations lets you set a custom order by adding a $migrationOrder property to your models. This is crucial for managing dependencies between tables.

Laravel Nova Compatibility

For those utilizing Laravel Nova, this package provides a streamlined command to create Laravel Nova resources directly tied to your models, enhancing your administrative panel’s integration.

Getting Started

Installation

To integrate Laravel Model Migrations into your project, simply run:

composer require will2therich/laravel-model-migrations

Usage

Implement the HasNewFactory trait in your models and define your migration and factory methods directly:

use will2therich\LaravelModelMigrations\Traits\HasNewFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Faker\Generator;

class MyModel extends Model
{
    use HasNewFactory;

    protected $guarded = [];
    protected $migrationOrder = 1; // optional

    public function migration(Blueprint $table)
    {
        $table->id();
        $table->string('name');
        $table->timestamps();
    }

    public function definition(Generator $faker)
    {
        return [
            'name' => $faker->name(),
            'created_at' => $faker->dateTimeThisMonth(),
        ];
    }
}

Migrating

To apply changes, use:

php artisan lmm:migrate {--f|--fresh} {--s|--seed}

Options include -f for fresh migrations and -s for running seeders after migrations.

Conclusion

Laravel Model Migrations by Weblabs Technologies LTD represents a significant leap forward in the efficiency of database management within the Laravel framework. By centralizing migration and factory definitions within models, developers can achieve faster iteration cycles and reduce complexity in their applications. Explore the package on GitHub or Packagist to see how it can streamline your development workflow.

Scroll to Top