HasOneThrough and HasManyThrough
Sometimes the data you need isn't directly related to a model — it's connected through another model in between. Rather than manually chaining multiple queries together, Eloquent offers "through" relationships to shortcut directly to that distant data.
What "Through" Relationships Solve
Consider a mechanic shop with mechanics, cars, and car owners: an owner doesn't directly relate to a mechanic, but the connection exists through the car. A "through" relationship lets a model reach that related data in one clean step, without manually looping through the intermediate model.
HasOneThrough
Used when the final relationship should return a single related record, reached via one intermediate model. It's defined with hasOneThrough(), specifying both the final and the intermediate model.
HasManyThrough
Used when the final relationship should return multiple related records through an intermediate model, defined with hasManyThrough().
| Relationship | Returns | Typical Use Case |
|---|---|---|
| hasOneThrough | A single related record | Getting a car's mechanic through its shop |
| hasManyThrough | A collection of related records | Getting all comments on a country's posts through its users |
- Both relationships require specifying the intermediate model explicitly
- They avoid writing manual joins or looping through intermediate results
- Foreign key names can be customized when they don't follow convention
Relationships make related data easy to reach, but fetching that data efficiently is a separate concern — which brings us to the difference between eager loading and lazy loading.
The Tables Behind a HasOneThrough
A through relationship only works when the foreign keys form a straight chain. For the mechanic example the tables look like this:
mechanics: id, name
cars: id, model, mechanic_id
owners: id, name, car_id
The mechanic has one car, and that car has one owner. The model method reads naturally:
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
class Mechanic extends Model
{
public function carOwner(): HasOneThrough
{
return $this->hasOneThrough(Owner::class, Car::class);
}
}
$owner = Mechanic::find(1)->carOwner; // a single Owner model or null
Behind the scenes, Eloquent builds one query that joins owners to cars on car_id and filters by cars.mechanic_id. There is no loop and no second query.
A HasManyThrough Example
For "all posts written by users from a country", the chain is countries to users to posts:
countries: id, name
users: id, name, country_id
posts: id, title, user_id
class Country extends Model
{
public function posts(): HasManyThrough
{
return $this->hasManyThrough(Post::class, User::class);
}
}
$posts = Country::find(5)->posts()
->where('published', true)
->latest()
->get();
Because posts() returns a query builder, you can add filters, ordering and pagination like any other relationship.
Notice that the posts table has no country_id column at all. That is exactly the point: you do not need to copy the country onto every post just to query by it. The intermediate users table already holds the link, and Eloquent uses it for you. Duplicating the column would only create a second source of truth that can drift out of sync when a user moves to another country.
You can also use through relationships inside existence checks, for example Country::has('posts')->get() to list only countries whose users have written at least one post, or withCount('posts') to show a post total next to each country.
Custom Keys and the Fluent Syntax
When column names do not follow convention, pass the keys in this order:
return $this->hasManyThrough(
Post::class, // final model
User::class, // intermediate model
'nation_id', // foreign key on users table
'author_id', // foreign key on posts table
'id', // local key on countries table
'id' // local key on users table
);
Recent Laravel versions also allow a readable alternative when the intermediate relationships already exist on the models: return $this->through('users')->has('posts');. It builds the same relationship from the existing users() and posts() methods.
Choosing the Right Relationship and Avoiding Errors
| Situation | Use |
|---|---|
| Chain of one-to-one links, one result | hasOneThrough |
| Chain ending in one-to-many, many results | hasManyThrough |
| Two models linked by a pivot table | belongsToMany |
| Chains longer than three tables | A package or a custom query |
- Keys in the wrong order: the third argument belongs to the intermediate table, the fourth to the final table. Swapping them returns empty results without an error.
- Wrong direction: through relationships follow hasOne or hasMany links outward from the parent; they do not walk backwards along belongsTo keys.
- Selecting ambiguous columns: both joined tables have an
id, so qualify columns, for exampleposts.id, in custom selects. - N+1 queries: when listing many countries with their posts, use
Country::with('posts')->get().
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.