Questions about laravel5.2 take
<code>$room=\App\Model\Room::with(['items'=>function($query){ $query->take(12); }])->get();</code>
In the above code, the items under each room are empty. When $query->take(12);
is commented out, the items appear again. Why?
Reply content:
<code>$room=\App\Model\Room::with(['items'=>function($query){ $query->take(12); }])->get();</code>
In the above code, the items under each room are empty. When $query->take(12);
is commented out, the items appear again. Why?
Well...the reason is actually very simple. You can add dd()
to see the SQL
it generates:
$room = \App\Model\Room::with(['items' => function($query){ dd($query->take(12)->toSql()); }])->get();
The following SQL
will be generated (? The number represents how many of your Room
there are):
select * from `machines` where `machines`.`series_id` in (?, ?, ?) limit 12
You can see that when you add restrictions such as take
and limit
, it does not limit each transaction
, but limits all the associated data obtained. Therefore, when you set a very small amount, it will Part of the Room
does not have Item
. You can set this number to a large value, such as 100000
, and you will find that items
has data, because your limit is greater than the amount of data.
As for how to limit the associated data of each transaction, I found some information but there is no simple way to achieve it. The most reliable one currently is this:
Tweaking Eloquent relations – how to get N related models per parent ?
I don’t understand what it means $room=AppModelRoom::with('items')->take(12);
Obviously, there was no match.
All with is implemented through the associated model abstract public function addEagerConstraints(array $models)
method.
You will find that the reason is that the data provided by the query of the associated data table in with
does not match the model data you obtained, resulting in no results for the query.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...
