Laravel Relationship并在结果集中为每个条目提供前几个条目

There is a car dealers listing with pagination where I need to show first 3 cars with that car's manufacturer and model no of that dealer. Its like

Car Dealer 1

  • car 1 -> Fetching car's Manufacturer from manufacturer table and model from model table
  • car 2 -> Fetching car's Manufacturer from manufacturer table and model from model table
  • car 3 -> Fetching car's Manufacturer from manufacturer table and model from model table

Car Dealer 2

  • car 1 -> Fetching car's Manufacturer from manufacturer table and model from model table
  • car 2 -> Fetching car's Manufacturer from manufacturer table and model from model table
  • car 3 -> Fetching car's Manufacturer from manufacturer table and model from model table

and so on with pagination

Its obvious that querying in loop is not a good solution. Is there any good work around this problem?

I have checked hasManyThrough but it does not suits here.

its kind a query : get first 3 cars with manufacturer and model nos for each dealers in resultset. If Laravel relationship does not work then what queries should be used to avoid looping? Thanks

You want to eager load the cars and their properties.

Dealer::with(['cars', 'cars.models', 'cars.makes'])->get();

Now you can iterate as much as you need to. The query counts won't increase.

Sorry for short answer. On phone :)