iamkevinmckee/many-to-many

Easily generate a migration for a many to many relationship

0.2.1 2023-05-04 20:16 UTC

This package is auto-updated.

Last update: 2024-11-04 23:19:22 UTC


README

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.