A Guide On Laravel Relationships
All you need to know about setting up and querying relationships
Defining relationships in Laravel is something that every Laravel developer probably has done more than once. But there are a lot of issues that you can run into when trying to implement a relationship. Since there are different types of relationships, how do you know which one to pick? And how can you use the full potential of relationships when it comes to down to querying the models?
Laravel relationships can be kind of hard to wrap your head around. If you don’t fully understand how relationships in Laravel work at this point, don’t worry. After reading this article you should have a better grasp of it.
What relationship should I use?
To answer this question you first have to know what the available options are. There are three different types of relationships:
- One to one
- One to many
- Many to many
We’ll go over the different types of relationships one by one and explain when to use them.
One to one
The one to one relationship is the most basic relation that exists. This type of relationship means that a model of type A may only be linked to one model of type B, and vice versa. For example, a relationship between a User model and a Passport model would be a one to one relationship. A user can only have one passport, and a passport only belongs to one user.
Let’s take a look at how we would define this relationship in code.
In the User model we create a method called
passport. We tell Laravel that the User model has one Passport by using the
All the methods that are used to define relationships have optional extra arguments. These arguments can be used to define the local and foreign key. By default Laravel will assume that you have a
passport_id defined in the User model, since you try to create a relationship with the Passport model. Keep this in mind when creating migration files!
In the Passport model we define the inverse of the relationship. We let the Passport model know that it belongs to a User. We do this by using the
One to many
The next relationship that you can define in Laravel is a one to many relationship. This type of relationship means that one model of type A may be linked to multiple models of type B. But the model of type B belongs to only one model of type A.
For example, a relationship between a User model and an Invoice model would be a one to many relationship. A user can have multiple invoices, but an invoice only belongs to one user.
In code this would look like this:
It looks just like the code that we saw earlier to define the one to one relationship, right?
All we have to do now is let the Invoice model know that it belongs to a User model. Let’s define the inverse of the one to many relationship.
Many to many
The last relationship is the many to many relationship. This type of relationship means that one model of type A may be linked to multiple models of type B, and vice versa.
For example, a relationship between an Invoice model and a Product model would be a many to many relationship. An invoice can have multiple products and a product can be on multiple invoices.
And you can define the inverse of this relationship like this:
Many to many relationships are slightly more difficult to implement because they require a junction table in the database. You can create this junction table in Laravel by creating a migration file.
Linking a model through another model
Has one through
The “has one through” relationship links models through a single intermediate relation. Imagine the situation where each product has one supplier and each product has one product history record. Then the supplier model can access the product history record through the product.
This is what the database tables look like:
Even though the
product_history table doesn’t contain a
supplier_id column, the supplier can get access to the
product_history record by using a “has one through” relationship.
The first argument that is passed to the
hasOneThrough method is the name of the final model. The second argument is the name of the intermediate model.
Has many through
The “has many through” relationship is the equivalent of the “has one through” relationship, but for multiple records. Let’s use the previous example, but we change one thing: a product can now have multiple history entries instead of one. The database tables stay the same.
This way the supplier model can get access to the history entries of the product.
Querying a relationship is pretty easy. Since we defined the one to one relationship for the passport and the one to many relationship for the invoice we can use them on the User model. On every instance of the User model we can now get the related passport and invoices.
It is also possible to query the inverse of the relationship. If you have an invoice you can get the user of that invoice.
Querying a many to many relationship works in the exact same way as the other relationships. In addition the many to many relationship has a
pivot attribute. This attribute represents the junction table and may be used like any other model.
For example, let’s say that the junction table has a
created_at column. We can get the
created_at property by using the pivot.
Querying the “has one through” and “has many through” works in exactly the same way as the other relationships.
It is possible to add constraints to a relationship while querying. Take a look at the following example:
Check if a relation exists
Sometimes you wish to check if a certain relation exists between models. Laravel has some very helpful methods to check this:
I hope that this article gave you a better understanding of Laravel relationships. Feel free to leave a comment if you have any feedback, questions or want me to write about another programming related topic.
Thanks for reading!