Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

craigivemy's avatar

Foreign Keys and Eloquent

I am new to Eloquent, and to Laravel actually. I am re-writing a project I built without a framework using the data mapper design pattern.

I am using migrations to build my tables again. A couple of these tables contain foreign key constraints, and others contain references to other tables but are not foreign keys as they are optional.

For example (1), my tasks table has a 'client_id' foreign key referencing clients(id), with cascade delete.

(2) But the tasks table also includes a 'department_id' column, which CAN be NULL, and no cascade action should take place on delete or update (so it's not been set as a foreign key of any sort), but if it is set, I know it references departments(id)

Here's where I'm not sure...

  1. Do I need to include the foreign keys in my migrations where there is a cascade action set? Does eloquent/my models need to know about these foreign keys?

  2. What about columns like department_id above? Or are these handled by setting the relationships in my models? Obviously they shouldn't be given cascade actions.

  3. Say I want to be able to list multiple department_id's, do I create a jointable or does Eloquent take care of this?

Thanks in advance.

0 likes
5 replies
Ricardo's avatar

@craigivemy welcome!

You can define your foreign keys in your migrations without problem. Eloquent does not use this information to load the relations.

You'll use something like this:

$tasks = Task::with('client', 'department')->get();

This will return a collection of tasks with the associated client and department for each task. You, then access the related models as:

$task = tasks::first();

$task->client->id;

$task->department->id;
1 like
pmall's avatar

Eloquent does not care the foreign keys and the constraints. If you create a department belongs to relationship in the Task model, it will execute a query to select the related department by its id, using tasks department_id value. Thats all.

1 like
craigivemy's avatar

Thanks guys. So actually SETTING foreign keys on columns in my migrations is only necessary where I need a delete action for example, to cascade? Still include this relationship on the Eloquent models presumably?

And for anything where no action is required on update or delete, just create the column 'department_id' for example, but don't set it as a foreign key, just set the relationship on the eloquent model?

How about if department_id could contain multiple values (one to many or many to many relationship) - this is where I'd use a jointable instead - correct?

pmall's avatar

Yes to all your question.

You have to realize eloquent doesn't actually read any info from your database structure, it is just an interface to build queries and retrieve objects based on how your model classes are set up.

ricardoarg's avatar

It's not obligatory to set your foreign keys, but nevertheless it's good practice to set them. You win referential integrity from the DB itself protecting against bad querys you could make, or bad querys eloquent could make because you set bad relations in your models.

Please or to participate in this conversation.