Laravel's Eloquent ORM provides a beautiful, simple implementation for working with your database. One of its most powerful features is the ability to define relationships between different models. Understanding relationships allows you to link database tables through Eloquent and run complex queries with minimal effort.
📁 What is a Relationship?
A relationship in Laravel refers to the connection between two or more Eloquent models (database tables). Relationships allow us to access related data using simple methods instead of writing complex SQL joins.
💼 When Should You Use Relationships?
Use relationships when:
- One model is logically related to another (e.g., a User has a Profile)
- You want to avoid writing raw SQL JOIN queries
- You want to access related records easily using model methods
1. hasOne
The hasOne
relationship defines a one-to-one connection where one model owns another.
For example, a User has one Profile.
// User.php
public function profile()
{
return $this->hasOne(Profile::class);
}
This means: the users
table is the parent, and each user has a related profile in the profiles
table.
2. belongsTo
The belongsTo
relationship is the inverse of hasOne
or hasMany
.
It indicates that the current model belongs to another model.
// Profile.php
public function user()
{
return $this->belongsTo(User::class);
}
This is defined in the child table (e.g., profiles) that holds the foreign key (user_id).
3. hasMany
The hasMany
relationship is used when one model owns multiple records of another model.
For example, a Post has many Comments.
// Post.php
public function comments()
{
return $this->hasMany(Comment::class);
}
Each post can have many comments in the comments
table with a post_id
foreign key.
4. belongsToMany (Many-to-Many)
This relationship is used when both models can have many of each other. For example, Users can have many Roles, and Roles can be assigned to many Users.
// User.php
public function roles()
{
return $this->belongsToMany(Role::class);
}
// Role.php
public function users()
{
return $this->belongsToMany(User::class);
}
This requires a pivot table (usually role_user
) with user_id
and role_id
columns.
5. hasOneThrough
This defines a one-to-one relationship that passes through an intermediate model. Example: A Country has one Leader, but the relation is through a Government model.
// Country.php
public function leader()
{
return $this->hasOneThrough(Leader::class, Government::class);
}
6. Polymorphic Relationships
These are used when multiple models can own a single model type. Example: Both Posts and Videos can have Comments.
// Comment.php
public function commentable()
{
return $this->morphTo();
}
// Post.php and Video.php
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
✅ Conclusion
Laravel Eloquent relationships simplify how you handle related data in your database. With simple methods, you can retrieve associated records and build powerful applications efficiently.
Start with basic relationships like hasOne
and hasMany
, and gradually explore more advanced types like polymorphic and through relationships.
💬 Have you used these relationships in your Laravel apps? Let me know in the comments!
Comments
Post a Comment