Menü schliessen
Created: June 27th 2025
Last updated: June 27th 2025
Categories: Databases,  IT Development,  Laravel
Author: Ian Walser

Laravel 12: What’s the Difference Between BelongsTo, HasOne, and HasMany?

Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Introduction

When working with Laravel 12, understanding Eloquent relationships is essential for building clean, efficient, and maintainable applications. Among the most commonly used relationships are BelongsTo, HasOne, and HasMany. But what exactly is the difference between them?

This guide will break down these relationship types with clear explanations and practical examples, so you can confidently use them in your Laravel projects.

Why Eloquent Relationships Matter

Laravel’s Eloquent ORM (Object-Relational Mapping) provides a clean and fluent interface to work with your database. Instead of writing complex SQL joins manually, you can define relationships between models and access related data seamlessly.

What Are Eloquent Relationships?

Eloquent relationships allow you to define how tables (models) relate to one another. The three types we’ll focus on are:

  • belongsTo: The current model holds the foreign key.
  • hasOne: The related model holds the foreign key.
  • hasMany: Like hasOne, but with multiple related records.

BelongsTo Relationship in Laravel

Definition

A "belongsTo" relationship defines the inverse of a "hasOne" or "hasMany". In other words, the current model "belongs to" another model.

Example Use Case

Let’s say you have a "Post" model and a "User" model. Each post belongs to a user.

Migration Example

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->constrained()->onDelete('cascade');
    $table->string('title');
    $table->text('content');
    $table->timestamps();
});

Model Relationship

In the "Post" model:

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

Usage

$post = Post::find(1);
echo $post->user->name;

HasOne Relationship in Laravel

Definition

A "hasOne" relationship indicates that the current model owns exactly one related model.

Example Use Case

Suppose a "User" has one "Profile".

Migration Example

Schema::create('profiles', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->constrained()->onDelete('cascade');
    $table->string('bio');
    $table->timestamps();
});

Model Relationship

In the "User" model:

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

Sample Output

Given the following "profiles" table data:

id user_id bio
1 1 Full-stack Laravel Developer

And the following code:

$user = User::find(1);
echo $user->profile->bio;

The output will be:

Full-stack Laravel Developer

HasMany Relationship in Laravel

Definition

A "hasMany" relationship signifies that the current model can own multiple instances of another model.

Example Use Case

A "User" can have many "Post"s.

Model Relationship

In the "User" model:

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

Sample Output

Given the following "posts" table data:

id user_id title
1 1 Laravel Eloquent Relationships
2 1 Understanding Blade Templates

And the following code:

$user = User::find(1);
foreach ($user->posts as $post) {
    echo $post->title . "<br>";
}

The output will be:

Laravel Eloquent Relationships
Understanding Blade Templates

Key Differences at a Glance

Relationship Direction Where Foreign Key Lives
belongsTo Child → Parent On the current model
hasOne Parent → Child On the related model
hasMany Parent → Children On the related model

When to Use Which Relationship

  • Use belongsTo when your model is the child and holds the foreign key (e.g., "Post" → "User")
  • Use hasOne when your model is the parent and you expect exactly one related record (e.g., "User" → "Profile").
  • Use hasMany when your model is the parent and you expect multiple related records (e.g., "User" → many "Post"s).

Common Mistakes to Avoid

  • Confusing the direction of the relationship (e.g., calling "hasMany" on a child).
  • Forgetting to set up foreign keys correctly in the database schema.
  • Not matching Laravel’s naming conventions, which could lead to the need to manually define foreign keys and local keys.

Conclusion

Understanding the difference between belongsTo, hasOne, and hasMany in Laravel 12 is crucial for building relational data models effectively. While "belongsTo" refers to a child pointing to a parent, "hasOne" and "hasMany" define relationships from the parent’s perspective.

By mastering these foundational Eloquent relationships, you'll write cleaner, more intuitive code — and make your Laravel applications more maintainable and powerful.