Skip to main content
Tutorial Free

A "hidden" way to drop foreign keys in migrations

February 15, 2016
1 min read
If we add a foreign key column in our migration file in function up(), we need to make sure that foreign key would be dropped in down() function, right? The thing is that you need to remember longer foreign key name to drop it by name. What official Laravel documentation doesn't say is that there's a more convenient way. Let's take a look at official Laravel documentation and what it says about foreign keys. 0215_laraveldaily_foreign_key As you can see, we are dropping foreign key with dropForeign() function and parameter which has this format: [table]_[column]_foreign. But there is another way to drop foreign key - just by the name of the field, you just have to put it as array parameter. So instead of
$table->dropForeign('posts_user_id_foreign');
We can just use this:
$table->dropForeign(['user_id']);
So, not everything is in the official documentation, sometimes dig deeper!

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.