koalaok koalaok - 11 days ago 7
PHP Question

Laravel many to many Relationship, is there a way to generate automatically pivot table?

I there possibly a way to have Laravel genrating automatically the table containing pivot indexes on a ManytoMany Relationship. Or maybe via artisan?

Until now I'm creating my Entity models (e.g. User and Role: a user has many roles and a roles is assumed by many user)
I define the belongsToMany method in each model.

Then I create migrations for create_users_table, create_roles_table, and create_role_user_table)

It would be great if there's a command that after creating the two Entities creates the third with n:n relationships table.

(Maybe the answer is goin to be : no it does not exists...)

Answer

Yes, using Laravel 5 Generators Extended package:

php artisan make:migration:pivot User Role

Install the package and then you will can use this and more new commands. Run php artisan after installing the package to see all the commands you can use.

Comments