What are Eloquent relationships (one-to-one, one-to-many, many-to-many)?
Jun 21, 2025 am 12:56 AMThe Eloquent relationship in Laravel is used to connect different database tables through a model to simplify associated data operations. One-to-one relationships such as user and information: the User model uses hasOne (Profile::class), and the Profile model uses belongsTo (User::class). One-to-many relationships such as articles and comments: The Post model uses hasMany(Comment::class), and the Comment model uses belongsTo(Post::class). Many-to-many relationships such as users and roles: both the User and Role models use the belongsToMany() method, and the relationship is managed through intermediate tables. You can add or remove associations using attach() or detach().
Eloquent relationships in Laravel are how you connect different database tables using models. They make it easier to work with related data without writing raw SQL joins every time.
One-to-One Relationships
This is when one record in a table is linked to exactly one record in another table.
For example, a User
might have one Profile
. To set this up in Eloquent:
- You create two models:
User
andProfile
. - In the
User
model, define a method calledprofile()
that returns$this->hasOne(Profile::class);
- In the Profile model, define a method called
user()
that returns$this->belongsTo(User::class);
By default, Eloquent assumes the foreign key is the model's name in snake_case plus _id
, like user_id
.
You can then do things like:
$user = User::find(1); echo $user->profile->bio;
This makes it easy to access related data without manually querying each time.
One-to-Many Relationships
This is when one record has many related records — like a Post
having many Comments
.
To set this up:
- The
Comment
model should belong to aPost
. - The
Post
model should define a method likecomments()
that returns$this->hasMany(Comment::class);
Then you can fetch all comments for a post easily:
$post = Post::find(5); foreach ($post->comments as $comment) { echo $comment->text; }
And if a comment has a post_id
, you can go the other way too:
$comment = Comment::find(1); echo $comment->post->title;
It's pretty common to use this for things like blog posts, orders per customer, etc.
Many-to-Many Relationships
This is when multiple records in one table related to multiple records in another — like Users
belonging to multiple Roles
, or Posts
connected to many Tags
.
To handle this, you need a pivot table (like role_user
or tag_post
). In Eloquent:
- A
User
model would have aroles()
method returning$this->belongsToMany(Role::class);
- And a
Role
model would have ausers()
method doing the same back.
Then you can grab related data like:
$user = User::find(1); foreach ($user->roles as $role) { echo $role->name; }
You can also attach or detach relationships:
- Attach:
$user->roles()->attach($roleId);
- Detach:
$user->roles()->detach($roleId);
This is super useful when dealing with tags, permissions, categories, and more.
That's the core idea behind these three Eloquent relationships. They're not hard once you see how they map real-world connections into code — just remember which model owns the foreign key and how the methods point to each other.
The above is the detailed content of What are Eloquent relationships (one-to-one, one-to-many, many-to-many)?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article brings you relevant knowledge about Laravel. It mainly introduces to you the implementation of optimistic locking in the Laravel Eloquent model. There are code examples. Friends who are interested can take a look below. I hope it will be helpful to you.

Laravel development: How to use LaravelEloquent to implement polymorphic associations? Polymorphic association is an important feature of Laravel Eloquent, which allows one model to establish relationships with multiple different models. In practical applications, processing different types of data is relatively simple and efficient, especially in database design. In this article, we will discuss how to implement polymorphic associations using Laravel Eloquent. 1. What is a polymorphic association? Polymorphism

Converting an array into an object using Eloquent in Laravel requires the following steps: Create an Eloquent model. Use Eloquent's select method to get the result and convert it to an array. Use ArrayObject to convert an array into an object. Gets an object property to access an array's values.

Laravel is a popular PHP framework that includes the powerful ORM (Object Relational Mapping) library-LaravelEloquent. This library is very powerful and can help us easily implement model association, making it easier to manage and query data. But many developers don't know how to use Laravel Eloquent to implement model association. In this article, I will introduce how to implement model association using Laravel Eloquent. 1. Laravel

Laravel is a popular PHP web framework that is popular due to its simplicity and ease of use. The Laravel framework is known for its excellent implementation of EloquentORM, an Object-RelationalMini mapping that supports the use of PHP to define database models and provides easy database interaction based on these models. This article will detail how to build a model using Laravel Eloquent to interact with the database quickly and reliably.

Laravel development: How to build a database model using LaravelEloquent? Laravel is a popular PHP framework that provides a powerful and easy-to-use database operation tool - Laravel Eloquent. In the past, using PHP to perform database operations inevitably required writing a large number of lengthy SQL statements and cumbersome codes. However, using Laravel Eloquent can easily build database models and achieve rapid development and maintenance. This article

As developers' needs for data interaction continue to grow, ORM has become an indispensable part of modern development. It can hide database operations in the background and provide a simplified API for CRUD operations. Among these ORM libraries, Eloquent has attracted the attention of many developers because it has been widely used in the Laravel framework. In PHP 8.0, Eloquent comes as a standalone extension library and can now be used in your projects. In this article we will explore Eloq

ThinkPHP6 is a very popular PHP framework, and Laravel is another popular PHP framework. Both frameworks have their own features and advantages, but Laravel's EloquentORM (Object Relational Mapping) engine is known as "the best ORM in the PHP world". What should we do if we want to use Laravel's EloquentORM when using ThinkPHP6? Let us discuss ThinkPHP in detail below
