laravel5.2 Take's question

Source: Internet
Author: User
$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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.