$room=\App\Model\Room::with(['items'=>function($query){ $query->take(12); }])->get();
The above code in each of the rooms under each of the items are empty, when the comment off $query->take(12); , items have, why?
Reply content:
$room=\App\Model\Room::with(['items'=>function($query){ $query->take(12); }])->get();
The above code in each of the rooms under each of the items are empty, when the comment off $query->take(12); , items have, why?
Well... The reason is really simple, you can add dd() a look at what it generates SQL :
$room = \App\Model\Room::with(['items' => function($query){ dd($query->take(12)->toSql()); }])->get();
will produce the following SQL (the number represents you Room have several):
select * from `machines` where `machines`.`series_id` in (?, ?, ?) limit 12
It is possible to see that the limitations of your affiliation, not the restriction take limit , but the total number of the connected numbers, so that 每一筆 when you set a very small amount, it will cause a part of the No Room Item , you can put it very large, for example 100000 , you itemsThere are numbers, because your limits are greater than the number of numbers.
As to how to limit the number of per-pen related figures, there is no easy way to find some information, and the most popular recipe is this:
Tweaking eloquent relations–how to get N related models per parent?
Do not understand what meaning $room =\app\model\room::with (' Items ')->take (12);
Apparently, there's no match.
All with is a method implemented through an association model abstract public function addEagerConstraints(array $models) .
You will find that the with query provided in the relational data table does not match the model data you obtained, resulting in no results for the query.