Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afoysal's avatar

One to Many to One relationship

How to do One to Many to One relationship in Laravel. I have three tables company, employee and motorcycle. One company has many employee. One employee has one motorcycle.

How can I do that Eloquent relationship in controller ?

0 likes
2 replies
rumm.an's avatar

Did you try hasManyThrough relationship. See docs.

In your Company model:

public function motorcycles()
{
     return $this->hasManyThrough(Motorcycle::class, Employee::class);
}

then you can retrive all the motorcycles within a company as:

$company->motorcycles;
//or eager load like this.
$companies = Company::with('motorcycles')->get();
1 like
Vilfago's avatar

Set your one to many (company -> employees) and one to one (employee -> moto) relation in the related models.

You can retrieve them with nested relations https://laravel.com/docs/5.6/eloquent-relationships#eager-loading

$companies = Company::with('employees.motorcycle')->get();

Has many through should work too, but will not retrieve the data of employees.

1 like

Please or to participate in this conversation.