Let’s dive in and learn how to model and also query different types of relationships in Laravel.
It is important to mention that Eloquent relationships are nothing new. They basically define the relationships between the tables in our relational databases.
But in addition, Eloquent gives us powerful methods we can use to query the different types of models relationships.
Our goal in this lessons is to:
- Define Eloquent model relationships
- Write codes to seed the database for all the tables we defined in the database
- Query the different types of relationships
Types of relationships covered
In this lesson, we’re going to learn the following types of relationships:
Eloquent methods to query the relationships
- hasOne and belongsTo
Watch the Tutorial (the rest of this article only gives the summary)
One To One
A One-to-One relationship is a simple type of relationship where a model (a row in the database table) can relate to only one record in a different table.
A good example of One to One is the relationship between User and Profile, such that a User can have only one Profile.
hasOne and belongsTo
We use the hasOne() and belongsTo() Eloquent methods to define the relationships of one to one in the Eloquent models.
One To Many
One-to-Many relationship is similar to one to one. One of the key differences is that in One-to-Many multiple rows in the “child” table can reference one row in the “parent” table.
A good example of One to Many relationship is the relationship between User and Comment. A user can post multiple comments but each comment belongs to one user.
hasMany and belongsTo
We use hasMany() and belongsTo() to define the One-to-Many relationship between the Eloquent models.
Many To Many
Many-to-Many relationship defines the relationship such that table X can reference zero or more rows in table Y, and table Y can reference zero or more rows from table X.
A good example of Many to Many relationships is the relationship between Post and Post Category. A post can belong to multiple categories, and a category can have multiple posts.
We use the belongsToMany() in the Eloquent models to define many to many relationships.
What is sometimes referred to as Pivot table, in the Laravel community, is an Intermediate Table that is used to define the many to many relationship between two other tables.
Has One Through
The Has-One-Through relationship is similar to One to One relationship. The major difference is that one of the tables in the relationship has no information about the table it is trying to relate to.
Take for example a profiles table that references users via user_id and then the users table has office_id that references offices table:
profiles => users => offices
Although Office has no direct relationship with the Profile, given an Office, we can e get the Profile through the User.
We use the Eloquent method hasOneThrough() to define the Has-One-Through relationship in the models.
Has Many Through
Has-Many-Through relationship is similar to Has-One-Through. The main difference is that the relationships between the indirectly connected tables will be a One to Many.
Take for example a posts table that references users via user_id and then the users table has office_id that references offices table:
posts => users => offices
A User can have many posts and given an Office, we can get all the indirectly related Posts through User.
We use the hasManyThrough() method to define Has-Many-Through relationship for Eloquent models.