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.
You can install the package via composer:
composer require iamkevinmckee/many-to-many --dev
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.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email mckee.kevin@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.