DEV Community

Cover image for Understanding Relationships in Eloquent (Laravel Beginner Guide)
Rohit Dhiman
Rohit Dhiman

Posted on

Understanding Relationships in Eloquent (Laravel Beginner Guide)

Relationships are one of the most important concepts in Laravel.

If you understand Eloquent Relationships properly, building real-world applications becomes much easier, cleaner, and more scalable.

Almost every application depends on connected data.

For example:

  • A user creates posts
  • A post has comments
  • An order belongs to a customer
  • Students enroll in courses

Instead of writing complicated SQL joins manually, Laravel provides elegant relationship methods through Eloquent ORM.

In this article, we’ll understand the most important relationships in Laravel with beginner-friendly examples.


What Are Eloquent Relationships?

Eloquent Relationships help models communicate with each other.

Think of relationships like connections between database tables.

Laravel provides methods that allow you to fetch related data easily.

Example:

$user->posts
Enter fullscreen mode Exit fullscreen mode

Instead of writing SQL joins manually, Laravel automatically handles the connection for you.


1. One to One Relationship

A One to One relationship means:

One record is connected to exactly one other record.

Example

A user has one profile.

Tables

users
profiles
Enter fullscreen mode Exit fullscreen mode

User Model

public function profile()
{
    return $this->hasOne(Profile::class);
}
Enter fullscreen mode Exit fullscreen mode

Profile Model

public function user()
{
    return $this->belongsTo(User::class);
}
Enter fullscreen mode Exit fullscreen mode

Usage

$user = User::find(1);

$user->profile;
Enter fullscreen mode Exit fullscreen mode

Real World Use Cases

  • User profile information
  • Employee details
  • Account settings

2. One to Many Relationship

This is the most commonly used relationship.

It means:

One record can have multiple related records.

Example

One user can create many posts.

User Model

public function posts()
{
    return $this->hasMany(Post::class);
}
Enter fullscreen mode Exit fullscreen mode

Post Model

public function user()
{
    return $this->belongsTo(User::class);
}
Enter fullscreen mode Exit fullscreen mode

Usage

$user->posts;
Enter fullscreen mode Exit fullscreen mode

Real World Examples

  • User → Posts
  • Category → Products
  • Post → Comments
  • Customer → Orders

3. Belongs To Relationship

belongsTo() is basically the inverse of hasMany() or hasOne().

It tells Laravel:

This model belongs to another model.

Example

A post belongs to a user.

Post Model

public function user()
{
    return $this->belongsTo(User::class);
}
Enter fullscreen mode Exit fullscreen mode

Usage

$post->user;
Enter fullscreen mode Exit fullscreen mode

Laravel automatically looks for:

user_id
Enter fullscreen mode Exit fullscreen mode

inside the posts table.


4. Many to Many Relationship

This relationship is used when both models can have multiple connections with each other.

Example

  • A student can join many courses
  • A course can contain many students

Student Model

public function courses()
{
    return $this->belongsToMany(Course::class);
}
Enter fullscreen mode Exit fullscreen mode

Course Model

public function students()
{
    return $this->belongsToMany(Student::class);
}
Enter fullscreen mode Exit fullscreen mode

Pivot Tables

Many to Many relationships require an extra table called a pivot table.

Example:

course_student
Enter fullscreen mode Exit fullscreen mode

This table stores:

student_id
course_id
Enter fullscreen mode Exit fullscreen mode

Usage Example

$student->courses;
Enter fullscreen mode Exit fullscreen mode

or

$course->students;
Enter fullscreen mode Exit fullscreen mode

Real World Examples

  • Roles ↔ Users
  • Products ↔ Tags
  • Students ↔ Courses
  • Movies ↔ Actors

5. Has Many Through Relationship

This relationship allows access to data through another model.

Example

A Country has many Posts through Users.

Structure

Country → Users → Posts
Enter fullscreen mode Exit fullscreen mode

Instead of manually joining tables, Laravel handles it.

Example

public function posts()
{
    return $this->hasManyThrough(
        Post::class,
        User::class
    );
}
Enter fullscreen mode Exit fullscreen mode

Useful For

  • Company → Employees → Tasks
  • School → Students → Results
  • Country → Users → Posts

This relationship is slightly advanced but very powerful.


Understanding Foreign Keys

Laravel relationships heavily depend on foreign keys.

Example:

users table → id
posts table → user_id
Enter fullscreen mode Exit fullscreen mode

Laravel automatically assumes naming conventions like:

model_name_id
Enter fullscreen mode Exit fullscreen mode

So following Laravel conventions saves a lot of time.


Eager Loading (Very Important)

One of the most important concepts related to relationships is Eager Loading.

The N+1 Query Problem

Imagine this:

$posts = Post::all();

foreach ($posts as $post) {
    echo $post->user->name;
}
Enter fullscreen mode Exit fullscreen mode

Laravel will run:

  • 1 query for posts
  • Multiple queries for users

This creates performance issues.


The Solution: Eager Loading

$posts = Post::with('user')->get();
Enter fullscreen mode Exit fullscreen mode

Now Laravel loads everything efficiently in fewer queries.

Benefits

  • Better performance
  • Faster applications
  • Reduced database load

If you are building scalable applications, eager loading is essential.


Common Beginner Mistakes

Here are mistakes many beginners make:

1. Wrong Foreign Key Names

Laravel expects:

user_id
Enter fullscreen mode Exit fullscreen mode

If you use different names, define them manually.


2. Forgetting Relationship Methods

This is wrong:

$user->posts()
Enter fullscreen mode Exit fullscreen mode

This is usually what you want:

$user->posts
Enter fullscreen mode Exit fullscreen mode

The first returns the relationship query builder.
The second returns actual data.


3. Not Using Eager Loading

Beginners often forget:

with()
Enter fullscreen mode Exit fullscreen mode

This can slow down applications badly.


4. Missing Pivot Tables

Many to Many relationships will not work properly without pivot tables.


Final Thoughts

Eloquent Relationships are one of Laravel’s strongest features.

Once you master them:

  • Your code becomes cleaner
  • Database queries become easier
  • Applications become more scalable
  • Development becomes faster

If you are learning Laravel, spend time practicing relationships with small projects.

That’s where things truly start connecting.


Quick Practice Challenge

Try building:

  • Blog system
  • Task manager
  • Student course portal
  • E-commerce mini project

And implement:

  • One to One
  • One to Many
  • Many to Many

Practice is the fastest way to understand relationships deeply.


Conclusion

Laravel relationships remove the pain of writing repetitive SQL queries manually.

They help developers focus more on application logic and less on database complexity.

Start simple.
Practice consistently.
And relationships will eventually feel natural.

Happy coding!

Top comments (0)