Skip to content

Easily generate a migration for a many to many relationship

License

Notifications You must be signed in to change notification settings

iAmKevinMcKee/many-to-many

Repository files navigation

Generate Many to Many Migrations

When you want to make a many to many relationship in Laravel, you need to create a pivot table. This package gives you a command to generate the migration for that pivot table automatically.

Installation

You can install the package via composer:

composer require iamkevinmckee/many-to-many --dev

Usage

When you need to create a pivot table for a many to many relationship, just run the following command:

php artisan many-to-many FirstModel SecondModel

For example:

php artisan many-to-many tag post

This will generate a migration with the following up method:

public function up()
{
    Schema::create('post_tag', function (Blueprint $table) {
        $table->unsignedBigInteger('post_id');
        $table->unsignedBigInteger('tag_id');
        $table->foreign('post_id')->references('id')->on('posts')
            ->onDelete('cascade');
        $table->foreign('tag_id')->references('id')->on('tags')
            ->onDelete('cascade');
    });
}

Then you only need to run the migrate command and add the relationship to the models.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email mckee.kevin@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

About

Easily generate a migration for a many to many relationship

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages