0

Sorry if the question isn't clear.. I'm not super fluent in databases.

I have three tables:

 companies       equipment          parts
-----------     -----------      -----------
id               id               id
name             company_id       equipment_id

Using Eloquent, how do I get a Collection of all the parts that belong to the company with id=1

I know you setup relationships in the model. So now I can get all a company's equipment ($myCompany->equipment), and all equipment's parts ($myEquipment->parts), but I'm not sure how to easily get values in the reverse direction two tables away.

Thanks!

Sandy
  • 2,572
  • 7
  • 40
  • 61

1 Answers1

2

Laravel has such a beautiful documentation and API for this very thing. Take a look at the hasManyThrough relationship.

So in your Company model add this:

/**
 * Get all of the parts for the company.
 */
public function parts()
{
    return $this->hasManyThrough('App\Part', 'App\Equipment');
}
nakov
  • 13,938
  • 12
  • 60
  • 110