iamkevinmckee/many-to-many

Easily generate a migration for a many to many relationship
1,552 4
Install
composer require iamkevinmckee/many-to-many
Latest Version:0.2.1
PHP:^7.1|^8.0
License:MIT
Last Updated:May 4, 2023
Links: GitHub  ·  Packagist
Maintainer: iamkevinmckee

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.

Related Packages

illuminatech/sync-many-attribute

Allows control over Eloquent many-to-many relation via array attribute

6,744 15
a2zwebltd/auditable-relations

Automatic auditing for Eloquent relationship changes (attach, detach, sync) usin...

3,078 4
doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

637,827,751 9,703
laravel/framework

The Laravel Framework.

589,172,971 34,950