简体   繁体   中英

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 (eg 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...)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM