Adding foreign keys can sometimes be tricky. You might get an error message or see that it doesn't work as expected. There are 3 common mistakes that we see developers make when adding foreign keys to their databases.
When defining a database relationship, you can use the `foreignId()` or `foreignIdFor()` methods to add a foreign key. Let's take a look at the examples.
While working on migration files in Laravel, it's relatively easy to forget one small detail which will restrict you from creating foreign keys. Let me tell you more.
There are a lot of sub-topics in this video. Why do you need foreign keys? What happens if you don't create them? How to override default errors? Check for child records on the front-end and the back-end? Use if-else or try-catch? Let's dive in.
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.