Updated April 10, 2023
Introduction to Laravel Eloquent Relationships
There is often a prime relation between database tables. For instance, if there are any comments for a particular blog post or a user has made an order, a related link gets created. Laravel Eloquent Relationships manage such relationships, which are of different types. Different types of relationships exist, and they are all managed excellently. The primary and reference keys manage the database tables and their relationships or links. Data is stored in different tables, and the relational database is split with Laravel Eloquent Relationships’ help.
What exactly are Laravel Eloquent Relationships?
Laravel is an open-source and very popular, and extensively used PHP framework. There is an Object-Relational Mapper, which Laravel has, namely, Eloquent. It is used to work with the database. It deals with database queries and provides simple syntax to have complex queries in a few seconds. Therefore, it helps you not to write long queries.
In the context of the existing database, Laravel Eloquent primarily has relationships to handle the relationship between the different tables. It uses the primary key and the reference key to manage the relationships. The database tables are often associated with each other and therefore have connections and relations. It can be possible that any user may have many profiles out there, or there can be any happening or an event that is related to a particular user. Laravel Eloquent is designed to manage such relationships smoothly without much hectic work or complex coding. The users benefit because it easily supports many different kinds of relationships that exist.
The different types of Laravel Eloquent Relationships that exist:
- One to One
- One to Many (Inverse also)
- Many to Many
- Has One Through
- Has Many Through
- One to One (Polymorphic)
- One to Many (Polymorphic)
- Many to Many (Polymorphic)
How do the Laravel Eloquent Relationships work in Laravel?
Relationships also act as a very powerful query builder like the Eloquent Models. Method chaining and the query capabilities can be used once Relationships are defined as a method. As listed above, the many different types of relationships that Laravel Eloquent Relationships provide are available for different needs and approaches that a user may be interested in, according to the data classification that the user may have done. Let us look at each of them to understand the Laravel Eloquent Relationships in a better manner.
1. One to One Relationship in Laravel
It is a crucial relationship in Laravel. Here, one table is associated with another, a single table available in the user-provided data. For example, if a user would like to create a profile of one’s self, then the person may create one user account, and on the other hand, the profile or the user account can be created by only that person. This is a classification of the data and building the relationship between two tables that define the user and the account/profile created. The example is here below:
Code:
class User {
public function profile() {
return $this -> hasOne(Profile::class);
}
}
class Profile {
public function user() {
return $this -> belongsTo(User::class);
}
}
2. One to Many Relationship in Laravel
This relationship is understood and applied when a single table is associated with many other tables. If a user wants to have multiple profiles, then this type of relationship is made to exist. When you place a method on an Eloquent model, this model of one to many can be easily defined. In this case, a user may create multiple profiles; however, the profile can be created only by one person. An appropriate sample to understand this is here below:
Code:
class User {
public function profiles() {
return $this -> hasMany(Profile::class);
}
}
class Profile {
public function user() {
return $this -> belongsTo(User::class);
}
}
3. Many to Many Relationships in Laravel
There is a little additional complexity here. If there are multiple mobile phones which an individual has and the mobile phones he possesses are also used by another few multiple persons. This type of relationship is understood to exist, and Laravel provides the option to handle this relationship and its database. In this case, the user may possess many phones, and these many phones, on the other hand, are also used by multiple other users. Laravel Eloquent manages the relational data with the help of Relationships. A clear example is here below:
Code:
class User {
public function phones() {
return $this -> belongsToMany(Phone::class);
}
}
class Phone {
public function users() {
return $this -> belongsToMany(User::class);
}
}
Understanding further the Laravel Eloquent Relationships
Dynamic Properties are there, but they are counted as lazy loading as they are slow. As a result, the relationship data will take time to register and analyze the relationship perfectly. Therefore, developers have commonly begun using eager loading instead to speed up the loading of the relationship of the data in the database. With this, the relationship is pre-loaded, and there is an assurance that the relationship can be easily accessed after properly loading the model. In addition, there is a very high intensity of decrease in the number of SQL queries when using eager loading. Therefore the loading is quicker, and the database is related quickly without much delay.
There are many other ways to execute actions to filter the necessary data with the help of the types of relationships and the links made to connect the relationship. Usually, this helps developers quickly arrange and organize data as per requirement.
Conclusion
The above understanding of Laravel Eloquent Relationships helps us to get a clear idea of the functioning of the Relationships and the various available options to meet the need of the users to organize and arrange data and also build the relationships as per the requirement to give an appropriate structure to the database automatically without too much of manual organization. Therefore, it saves the developers a lot of time and energy and avoids mistakes as it is automated and organized well.
Recommended Article
We hope that this EDUCBA information on “Laravel Eloquent Relationships” was beneficial to you. You can view EDUCBA’s recommended articles for more information.