Eloquent Relationships Explained
with Real Examples
“One-to-one, one-to-many, many-to-many, polymorphic…” — Laravel’s Eloquent ORM makes working with relationships a breeze. This guide explains every relationship type with real-world code examples, so you can model your data like a pro.
📑 Table of Contents
1. Introduction
Laravel’s Eloquent ORM provides a beautiful, expressive way to interact with your database. At the heart of Eloquent are relationships — the ability to define how your models relate to one another. Whether you're building a blog, an e-commerce store, or a social network, understanding relationships is essential for clean, efficient code.
This guide covers every relationship type Laravel offers, with real examples you can immediately apply. We'll also dive into eager loading to avoid the N+1 query problem, and share best practices to keep your code maintainable.
with(), whereHas(),
and load().
2. One-to-One
A one-to-one relationship occurs when one model is associated with exactly one
other model. For example, a User has one Profile, and a Profile
belongs to one User.
📌 Real Example: User & Profile
Usage:
$user = User::find(1);
$profile = $user->profile; // retrieves the related Profile
user_id on the profiles
table. You can customize it by passing a second argument.
3. One-to-Many
A one-to-many relationship is the most common. For instance, a User
can have many Posts, and each Post belongs to one User.
Usage:
$posts = User::find(1)->posts; // collection of posts
$user = Post::find(10)->user; // the user who authored the post
4. Many-to-Many
Many-to-many relationships are more complex and require a pivot table.
For example, a Post can have many Tags, and a Tag
can be attached to many Posts.
Pivot table: post_tag with columns post_id and tag_id.
📌 Accessing Pivot Data
You can retrieve extra columns from the pivot table using withPivot().
5. Has-Many-Through
The hasManyThrough relationship provides a convenient shortcut for accessing
distant relations via an intermediate model. For example, a Country has many
Posts through its Users.
This assumes the users table has a country_id column and the
posts table has a user_id column.
6. Polymorphic Relations
Polymorphic relationships allow a model to belong to more than one other model on a single association.
For example, Comments can belong to Posts or Videos.
📌 Polymorphic One-to-Many
Table structure: comments table needs commentable_id
and commentable_type columns.
📌 Polymorphic Many-to-Many
Laravel also supports polymorphic many-to-many, useful for tagging or favoriting.
For instance, Tags can be attached to Posts and Videos
via a polymorphic pivot table.
7. Eager Loading (Avoid N+1)
By default, Eloquent loads relationships lazily — which means the related data is only fetched when you access it. This can lead to the N+1 query problem, where you execute an extra query for each record.
Solution: Use with() to eager load relationships in advance.
You can also eager load nested relationships: Post::with('user.profile').
9. Best Practices
Follow these tips to get the most out of Eloquent relationships:
- Always define inverse relationships: If you define
hasMany, also define the correspondingbelongsToon the other model. - Name your relationships clearly: Use plural for
hasMany, singular forhasOneandbelongsTo. - Eager load by default when appropriate: Use
with()in your model'sboot()method or in repository classes. - Use
whereHas()for filtering: Query based on relationship existence without loading the related data. - Leverage pivot models: Use
using()to define a custom pivot model for many-to-many relationships when you need additional logic. - Optimize with
select(): Only fetch the columns you need to reduce memory usage.

0 Comments
thanks for your comments!