Skip to main content

Overview

Database tables are often related to one another. A blog post has many comments; an order belongs to a user. Eloquent makes it easy to define these relationships and query related data in a natural, expressive way. Relationships are defined as methods on your Eloquent model classes.
Examples on this page use User, Post, Comment, and Tag models. Assume these models and their database tables already exist.
The main relationship types Eloquent supports:

hasOne (one-to-one)

Use hasOne when a model owns exactly one related model. For example, a User has one Profile.

Defining the relationship

Eloquent assumes the profiles table has a user_id foreign key based on the parent model name. Access the relationship as a property — Eloquent queries the database automatically:
Calling a relationship method as a property is called a “dynamic relationship property”. Eloquent runs the query and returns the result the first time you access it.

belongsTo (inverse of hasOne)

belongsTo is the inverse of hasOne. Define it on the model that holds the foreign key — in this case, Profile belongs to User.
Eloquent uses the method name plus _id as the foreign key (user_id).

hasMany (one-to-many)

hasMany is the most common relationship type. Use it when a parent model owns multiple child models — for example, a Post has many Comment records.

Defining the relationship

Eloquent assumes the comments table has a post_id foreign key. A hasMany relationship returns a collection:
Chain query constraints by calling the relationship as a method:

Inverse relationship

To navigate from a comment back to its post, define belongsTo on Comment:

Filtering with whereBelongsTo()

When retrieving the children of a belongsTo relationship, you could manually specify the foreign key with where('post_id', $post->id), but the whereBelongsTo method automatically determines the proper relationship and foreign key for the given model:
You may also pass a collection instance. In that case, Laravel retrieves models that belong to any of the parent models within the collection:
By default the relationship is determined from the class name of the given model, but you may specify the relationship name explicitly as the second argument:

Automatic parent hydration with chaperone()

Even after eager loading with Post::with('comments'), accessing $comment->post on each child triggers an additional query, leading to an N+1 problem. Adding chaperone() to the hasMany definition makes Eloquent automatically hydrate the parent model onto each child, avoiding this extra query.
If you’d rather enable this only at query time without changing the relationship definition, call chaperone() inside with():
chaperone() works the same way for polymorphic morphMany relationships. For chaperone() on belongsToMany pivot models, see Custom pivot models and chaperone.

belongsToMany (many-to-many)

Use a many-to-many relationship when both models can be associated with multiple instances of each other. For example, a Post can have many Tag records, and a Tag can belong to many posts.

Table structure

Many-to-many relationships require a pivot table. For Post and Tag, create a post_tag pivot table:
Eloquent infers the pivot table name by alphabetically combining the two model names: post + tagpost_tag.

Defining the relationship

Define the inverse on Tag to navigate in both directions:

Attaching and detaching

Use attach() to add a record to the pivot table, detach() to remove one:
sync() removes any pivot rows not included in the given array and inserts the ones that are missing, leaving you with exactly the specified set of associations.
For advanced usage — storing extra columns on the pivot table or specifying a custom pivot model with using() — see Custom pivot models and chaperone.

Eager loading

The N+1 problem

When you access a relationship as a property, Eloquent runs a separate query each time. Inside a loop this creates an N+1 problem:
With 100 posts, this runs 101 queries, which has a significant impact on performance.

Eager loading with with()

Use with() to load all related data in just two queries:
The two queries are:

Loading multiple relationships

Pass an array to eager-load several relationships at once:

Nested eager loading

Use dot notation to eager-load nested relationships:
Eager loading is essential for preventing performance problems. Make it a habit to use with() whenever you loop over models and access relationships.

Next steps

Authentication

Learn how to add login and registration to your Laravel application.
Last modified on September 11, 2026