Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Role Model/Migrations, Seeder and Teacher Registration

Premium
3 min read

Comments & Discussion

O
oluchristian ✓ Link copied!

After runing migrations, php artisan command to add role_id to users table was successful (I confirmed this using: php artisan migrate:status ) but when i chek mysql users database, I am unable to find the foreign key of role_id in users table.

I had to start the whole project again to be sure it wasn't my fault. I have also done research online as to why this is happening and also watched your youtube video titled, "Laravel Migrations: Table created but foreign key failed" but no luck

PK
Povilas Korop ✓ Link copied!

Can you show me the code of your migration file for this?

Should be:

$table->foreignId('role_id')->constrained();
O
Oleksii ✓ Link copied!

is it justified to create separate table only for 2-10-N roles ? perhaps consts or enum is better ?

M
Modestas ✓ Link copied!

Yes! The key aspect when creating a separate table for roles is - flexibility. Sure, const or enum can do similar things, but over teh long run - you never know what will be needed :)