Skip to main content

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

Read more here
Premium Members Only
Join to unlock this tutorial and all of our courses.
Tutorial Premium Tutorial

Eloquent Foreign Keys: 3 Common Mistakes to Avoid

March 05, 2023
7 min read

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:

  • Forgetting to add Constraints
  • Adding new Foreign Keys Without Proper Default Data
  • Allowing to Delete Parent Records when Child Record Exists

Let's see those mistakes and how to fix them!


Mistake 1: Forgetting to add Constraints

Database constraints are a valuable feature we should not ignore. They help us protect from missing related records in our database.

Let's build a basic example here:

Migration

// Create Categories table
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('category')->nullable();
$table->timestamps();
});
 
// Create a child Questions table with a relationship to the parent Categories table
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->foreignId('category_id');
$table->longText('question')->nullable();
$table->longText('answer')->nullable();
$table->timestamps();
});

You might think that we are good to go here. However, we are not. It allows us to create new Questions with a category that doesn't exist:

$question = Question::create([
'category_id' => 155,
'question' => 'How to use Laravel?',
'answer' => 'You can use Laravel by following the documentation',
]);

And this will work perfectly fine. Why? Because we don't tell our database to check for the category id.

We have 0 categories in our database, but we can still create a new Question with category id 155. It is not good. Let's fix it!

Migration

// ...
 
// Create a child Questions table with a relationship to the parent Categories table
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->foreignId('category_id')->constrained();
$table->longText('question')->nullable();
$table->longText('answer')->nullable();
$table->timestamps();
});

When we try to run the same code of Question::create(), we get an error...

Premium Members Only

This advanced tutorial is available exclusively to Laravel Daily Premium members.

Premium membership includes:

Access to all premium tutorials
Video and Text Courses
Private Discord Channel

Comments & Discussion

AB
Alejandro Biedma ✓ Link copied!

Hey there! Great article! Thanks for sharing it. But...is it me, or am I seeing the same example in Mistake 1, when it is suppose to show the correct code? I think it is the same sample code that in the first part. Thanks!

PK
Povilas Korop ✓ Link copied!

Of course! Well spotted, lost it in the editing/reviewing process, fixed now, the mistake was NOT using constrained().

LN
Loganathan Natarajan ✓ Link copied!

Useful article!

K
Kubot ✓ Link copied!

Labai praverte darant baigiamaji codeacademi darba. Visi trys atvejai buvo kur buvo kile klausimu.

JP
Janos Peter ✓ Link copied!

Great article!

O
Oleksandr ✓ Link copied!

Hi! Great article. But I saw a mistake in the last example. We load the count in the controller but in the template use queries to count. I think we need to use count property @if($category->questions_count === 0)

M
Modestas ✓ Link copied!

Thank you for spotting this! Updating the example

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.