Eloquent Relationships Explained (Laravel)
Eloquent relationships let a Laravel model define how it relates to other models — one-to-one, one-to-many, many-to-many, and more — as simple method calls i...
Published November 22, 2026
Eloquent relationships let you define how models relate (one-to-one, one-to-many, many-to-many) and then access related records as properties, such as $user->posts. Laravel generates the joins and queries for you.
The concept
Define a method on the model returning hasOne, hasMany, belongsTo or belongsToMany. Accessing it as a property lazy-loads the data; calling it as a method returns a query builder.
Avoid N+1 queries with eager loading (with()) and use withCount for totals.
Common types
Practical examples
Define
class User extends Model {
public function posts() { return $this->hasMany(Post::class); }
}
class Post extends Model {
public function user() { return $this->belongsTo(User::class); }
public function tags() { return $this->belongsToMany(Tag::class); }
}Use
$user->posts; // collection
$user->posts()->where('published', 1)->get();
$post->tags()->attach([1, 2]);Eager load to avoid N+1
$posts = Post::with('user', 'tags')->latest()->paginate(20);Counts
User::withCount('posts')->get(); // posts_countCommon mistakes
- N+1 queries from loops that touch relations.
- Forgetting the foreign key naming convention.
- Loading huge relations without constraints or pagination.
Key takeaways
- Relationships are model methods.
- property = results, method = query.
- Eager load with with().
FAQ
hasOne vs belongsTo?
hasOne is on the model that does not hold the foreign key; belongsTo is on the one that does.
Polymorphic relations?
morphTo/morphMany let one model belong to several others.