{"id":501110,"date":"2026-10-31T09:30:13","date_gmt":"2026-10-31T06:30:13","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=501110"},"modified":"2026-10-31T09:30:13","modified_gmt":"2026-10-31T06:30:13","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=501110","title":{"rendered":"N+1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 Laravel: \u043a\u0430\u043a \u043e\u0434\u0438\u043d foreach \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 101 SELECT \u0438 \u043a\u0430\u043a \u044d\u0442\u043e \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f: \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0438\u0437 100 \u043f\u043e\u0441\u0442\u043e\u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e, \u0430 \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u0441 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u2014 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442. \u041f\u0440\u043e\u0444\u0430\u0439\u043b\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0435 \u043e\u0434\u0438\u043d \u0442\u044f\u0436\u0451\u043b\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441, \u0430 \u0441\u043e\u0442\u043d\u044e \u043b\u0451\u0433\u043a\u0438\u0445 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445. \u042d\u0442\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N+1, \u0438 \u0432 Laravel \u0435\u0451 \u043e\u0447\u0435\u043d\u044c \u043b\u0435\u0433\u043a\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e Eloquent \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0441\u0432\u044f\u0437\u0438 \u043b\u0435\u043d\u0438\u0432\u043e.<\/p>\n<h3>\u041e\u0442\u043a\u0443\u0434\u0430 \u0431\u0435\u0440\u0451\u0442\u0441\u044f N+1<\/h3>\n<pre><code class=\"php\">\/\/ app\/Models\/Post.phpclass Post extends Model{    public function author(): BelongsTo    {        return $this-&gt;belongsTo(User::class, 'user_id');    }    public function comments(): HasMany    {        return $this-&gt;hasMany(Comment::class);    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper\"><\/span><\/a><\/div><\/pre>\n<pre><code class=\"php\">$posts = Post::latest()-&gt;take(100)-&gt;get();   \/\/ 1 \u0437\u0430\u043f\u0440\u043e\u0441foreach ($posts as $post) {    echo $post-&gt;author-&gt;name;                 \/\/ +1 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u041a\u0410\u0416\u0414\u042b\u0419 \u043f\u043e\u0441\u0442}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a <code>$post-&gt;author<\/code> \u2014 \u044d\u0442\u043e \u043d\u0435 \u043f\u043e\u043b\u0435, \u0430 \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 <code>SELECT * FROM users WHERE id = ?<\/code>. \u0418\u0442\u043e\u0433\u043e 1 \u0437\u0430\u043f\u0440\u043e\u0441 \u0437\u0430 \u0441\u043f\u0438\u0441\u043e\u043a + N \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0437\u0430 \u0430\u0432\u0442\u043e\u0440\u043e\u0432.<\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/extt\/12\/04\/bf\/1204bf006a11c72d39d457622019dd79.png\" alt=\"img1-diagram\" sizes=\"(max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/extt\/12\/04\/bf\/1204bf006a11c72d39d457622019dd79.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/extt\/12\/04\/bf\/1204bf006a11c72d39d457622019dd79.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>img1-diagram<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 Blade \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0435\u0449\u0451 \u043d\u0435\u0432\u0438\u043d\u043d\u0435\u0435 \u2014 <code>{{ $post-&gt;author-&gt;name }}<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>@foreach<\/code>, \u0438 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u00ab\u0437\u0430\u043f\u0440\u043e\u0441\u0430\u00bb \u0433\u043b\u0430\u0437\u0430\u043c\u0438 \u043d\u0435 \u0432\u0438\u0434\u043d\u043e.<\/p>\n<h3>\u041a\u0430\u043a \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443<\/h3>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0431\u0435\u0437 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u2014 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<pre><code class=\"php\">use Illuminate\\Support\\Facades\\DB;DB::enableQueryLog();$posts = Post::latest()-&gt;take(100)-&gt;get();foreach ($posts as $post) {    $post-&gt;author-&gt;name;}dump(count(DB::getQueryLog())); \/\/ 101<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u0418\u043b\u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 dev-\u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0438:<\/p>\n<pre><code class=\"php\">\/\/ AppServiceProvider::boot()DB::listen(function ($query) {    logger()-&gt;debug($query-&gt;sql, $query-&gt;bindings);});<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u0423\u0434\u043e\u0431\u043d\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u2014 Laravel Debugbar \u0438\u043b\u0438 Telescope: \u0442\u0430\u043c \u0441\u0440\u0430\u0437\u0443 \u0432\u0438\u0434\u043d\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f SQL.<\/p>\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u0435: eager loading<\/h3>\n<pre><code class=\"php\">$posts = Post::with('author')-&gt;latest()-&gt;take(100)-&gt;get();foreach ($posts as $post) {    echo $post-&gt;author-&gt;name;   \/\/ \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c Eloquent \u0434\u0435\u043b\u0430\u0435\u0442 \u0440\u043e\u0432\u043d\u043e \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430: \u043f\u043e\u0441\u0442\u044b \u0438 <code>SELECT * FROM users WHERE id IN (...)<\/code>, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u0430\u043c \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u043f\u043e \u043c\u043e\u0434\u0435\u043b\u044f\u043c.<\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/extt\/f3\/74\/5f\/f3745f2e08772d1fea8229b3c02a76f8.png\" alt=\"img2-before-after\" sizes=\"(max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/extt\/f3\/74\/5f\/f3745f2e08772d1fea8229b3c02a76f8.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/extt\/f3\/74\/5f\/f3745f2e08772d1fea8229b3c02a76f8.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>img2-before-after<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b:<\/p>\n<pre><code class=\"php\">\/\/ \u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0432\u044f\u0437\u0435\u0439 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0441\u0432\u044f\u0437\u0438Post::with(['author', 'comments.author'])-&gt;get();\/\/ \u0422\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u043d\u044b\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0438 (\u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447 id \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d!)Post::with('author:id,name')-&gt;get();\/\/ \u041d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u2014 \u043d\u0435 \u0433\u0440\u0443\u0437\u0438\u043c \u0441\u0430\u043c\u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438Post::withCount('comments')-&gt;get();   \/\/ $post-&gt;comments_count\/\/ \u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u0443\u0436\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430 \u2014 \u0434\u043e\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0441\u0432\u044f\u0437\u044c \u043e\u0434\u043d\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c$posts-&gt;load('author');$posts-&gt;loadMissing('author');        \/\/ \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0435\u0449\u0451 \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u0447\u0435\u043c\u0443 withCount \u043b\u0443\u0447\u0448\u0435, \u0447\u0435\u043c $post-&gt;comments-&gt;count()<\/summary>\n<div class=\"spoiler__content\">\n<p><code>$post-&gt;comments-&gt;count()<\/code> \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 <strong>\u0432\u0441\u0435<\/strong> \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c (\u0438 \u0431\u0435\u0437 <code>with<\/code> \u2014 \u0435\u0449\u0451 \u0438 N \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432), \u0430 \u043f\u043e\u0442\u043e\u043c \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. <code>$post-&gt;comments()-&gt;count()<\/code> \u0441\u043e \u0441\u043a\u043e\u0431\u043a\u0430\u043c\u0438 \u0434\u0435\u043b\u0430\u0435\u0442 <code>SELECT COUNT(*)<\/code> \u2014 \u043d\u043e \u0442\u043e\u0436\u0435 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043d\u043e\u0432\u0430 N+1.<\/p>\n<p><code>withCount('comments')<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 SELECT, \u0438 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0433\u043e\u0442\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 <code>comments_count<\/code> \u0437\u0430 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<\/div>\n<\/details>\n<h3>\u041b\u043e\u0432\u0443\u0448\u043a\u0430: N+1 \u0432\u043d\u0443\u0442\u0440\u0438 accessor-\u043e\u0432 \u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432<\/h3>\n<p>\u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u0441\u0442\u043e\u0438\u0442 <code>with('author')<\/code>, N+1 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u0447\u0435\u0440\u0435\u0437 API Resource:<\/p>\n<pre><code class=\"php\">class PostResource extends JsonResource{    public function toArray($request): array    {        return [            'id'       =&gt; $this-&gt;id,            'title'    =&gt; $this-&gt;title,            \/\/ \u0435\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u044c \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430 \u2014 \u0437\u0434\u0435\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441            'author'   =&gt; new UserResource($this-&gt;whenLoaded('author')),            'comments' =&gt; $this-&gt;whenCounted('comments'),        ];    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p><code>whenLoaded()<\/code> \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0441\u0432\u044f\u0437\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0443\u0436\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430, \u0430 \u043d\u0435 \u0434\u0451\u0440\u0433\u0430\u0435\u0442 \u0431\u0430\u0437\u0443 \u043c\u043e\u043b\u0447\u0430.<\/p>\n<h3>\u0417\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u043c \u043b\u0435\u043d\u0438\u0432\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430<\/h3>\n<p>\u041b\u0443\u0447\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0435 \u043b\u043e\u0432\u0438\u0442\u044c N+1 \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u2014 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043f\u0430\u0434\u0430\u043b \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435. \u0421 Laravel 8.43 \u0435\u0441\u0442\u044c <code>Model::preventLazyLoading()<\/code>:<\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/extt\/17\/15\/18\/171518057731be67c435f4d175eeab66.png\" alt=\"img3-prevent\" sizes=\"(max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/extt\/17\/15\/18\/171518057731be67c435f4d175eeab66.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/extt\/17\/15\/18\/171518057731be67c435f4d175eeab66.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>img3-prevent<\/figcaption><\/div>\n<\/figure>\n<pre><code class=\"php\">\/\/ app\/Providers\/AppServiceProvider.phpuse Illuminate\\Database\\Eloquent\\Model;public function boot(): void{    Model::preventLazyLoading(! $this-&gt;app-&gt;isProduction());}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043b\u044e\u0431\u043e\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u043d\u0435\u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0432 dev \u0438 \u0442\u0435\u0441\u0442\u0430\u0445 \u0431\u0440\u043e\u0441\u0438\u0442 <code>LazyLoadingViolationException<\/code>. \u041d\u0430 \u043f\u0440\u043e\u0434\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u2014 \u043c\u043e\u0436\u043d\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c:<\/p>\n<pre><code class=\"php\">Model::handleLazyLoadingViolationUsing(function ($model, $relation) {    logger()-&gt;warning('Lazy loading', [        'model'    =&gt; get_class($model),        'relation' =&gt; $relation,    ]);});<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u0410 <code>Model::shouldBeStrict()<\/code> \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0441\u0440\u0430\u0437\u0443 \u0442\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438: \u0437\u0430\u043f\u0440\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438, \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0443 \u0438 \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043c\u043e\u043b\u0447\u0430 \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u043d\u0435\u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d\u043d\u043e\u0435 \u0434\u043b\u044f mass assignment \u043f\u043e\u043b\u0435.<\/p>\n<h3>\u0422\u043e\u0442 \u0436\u0435 N+1 \u2014 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 Eloquent<\/h3>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043d\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u0430 \u043a Laravel: \u0442\u043e\u0442 \u0436\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u00ab\u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0446\u0438\u043a\u043b\u0435\u00bb \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0447\u0438\u0441\u0442\u043e\u043c PHP \u0441 PDO, \u0432 Django ORM \u0438 \u0432 GraphQL-\u0440\u0435\u0437\u043e\u043b\u0432\u0435\u0440\u0430\u0445. \u0412 GraphQL \u0435\u0451 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0448\u0430\u044e\u0442 \u0447\u0435\u0440\u0435\u0437 DataLoader, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 id \u0438\u0437 \u0432\u0441\u0435\u0445 \u0440\u0435\u0437\u043e\u043b\u0432\u0435\u0440\u043e\u0432 \u0438 \u0434\u0435\u043b\u0430\u0435\u0442 \u043e\u0434\u0438\u043d \u0431\u0430\u0442\u0447-\u0437\u0430\u043f\u0440\u043e\u0441 \u2014 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u0435\u0441\u0442\u044c \u0432 \u0448\u043e\u0440\u0442\u0441\u0435:<\/p>\n<p><a href=\"https:\/\/www.youtube.com\/shorts\/o92gtJKXgMc\" rel=\"noopener nofollow\">https:\/\/www.youtube.com\/shorts\/o92gtJKXgMc<\/a><\/p>\n<h3>\u0427\u0435\u043a-\u043b\u0438\u0441\u0442<\/h3>\n<ul>\n<li>\n<p>\u0412\u0438\u0434\u0438\u0442\u0435 <code>$model-&gt;relation<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>foreach<\/code>\/<code>@foreach<\/code> \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435, \u0435\u0441\u0442\u044c \u043b\u0438 <code>with()<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0443\u0436\u043d\u043e \u0447\u0438\u0441\u043b\u043e \u2014 <code>withCount()<\/code>, \u0430 \u043d\u0435 <code>-&gt;count()<\/code> \u043f\u043e \u0441\u0432\u044f\u0437\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445 \u2014 <code>whenLoaded()<\/code> \/ <code>whenCounted()<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0412 <code>AppServiceProvider<\/code> \u2014 <code>Model::preventLazyLoading(! app()-&gt;isProduction())<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0442\u0435\u0441\u0442\u0430\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 <code>DB::getQueryLog()<\/code>.<\/p>\n<\/li>\n<\/ul>\n<hr\/>\n<p>\u0415\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c Eloquent, \u0441\u0432\u044f\u0437\u0438, \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0439 Laravel \u2014 \u0435\u0441\u0442\u044c \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u043a\u0443\u0440\u0441 \u0441 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u043e\u0439: <a href=\"https:\/\/academy.66soft.ru\/courses\/laravel?utm_source=habr&amp;utm_medium=article&amp;utm_campaign=n_plus_one\" rel=\"noopener nofollow\">Laravel \u043d\u0430 \u0421\u0443\u0431\u043a\u043e\u043d\u0442\u043e.\u0410\u043a\u0430\u0434\u0435\u043c\u0438\u0438<\/a>.<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1101990\/\">https:\/\/habr.com\/ru\/articles\/1101990\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f: \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0438\u0437 100 \u043f\u043e\u0441\u0442\u043e\u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e, \u0430 \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u0441 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u2014 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442. \u041f\u0440\u043e\u0444\u0430\u0439\u043b\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0435 \u043e\u0434\u0438\u043d \u0442\u044f\u0436\u0451\u043b\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441, \u0430 \u0441\u043e\u0442\u043d\u044e \u043b\u0451\u0433\u043a\u0438\u0445 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445. \u042d\u0442\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N+1, \u0438 \u0432 Laravel \u0435\u0451 \u043e\u0447\u0435\u043d\u044c \u043b\u0435\u0433\u043a\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e Eloquent \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0441\u0432\u044f\u0437\u0438 \u043b\u0435\u043d\u0438\u0432\u043e.\u041e\u0442\u043a\u0443\u0434\u0430 \u0431\u0435\u0440\u0451\u0442\u0441\u044f N+1\/\/ app\/Models\/Post.phpclass Post extends Model{    public function author(): BelongsTo    {        return $this-&gt;belongsTo(User::class, &#8216;user_id&#8217;);    }    public function comments(): HasMany    {        return $this-&gt;hasMany(Comment::class);    }}$posts = Post::latest()-&gt;take(100)-&gt;get();   \/\/ 1 \u0437\u0430\u043f\u0440\u043e\u0441foreach ($posts as $post) {    echo $post-&gt;author-&gt;name;                 \/\/ +1 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u041a\u0410\u0416\u0414\u042b\u0419 \u043f\u043e\u0441\u0442}\u041e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a $post-&gt;author \u2014 \u044d\u0442\u043e \u043d\u0435 \u043f\u043e\u043b\u0435, \u0430 \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 SELECT * FROM users WHERE id = ?. \u0418\u0442\u043e\u0433\u043e 1 \u0437\u0430\u043f\u0440\u043e\u0441 \u0437\u0430 \u0441\u043f\u0438\u0441\u043e\u043a + N \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0437\u0430 \u0430\u0432\u0442\u043e\u0440\u043e\u0432.img1-diagram\u0412 Blade \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0435\u0449\u0451 \u043d\u0435\u0432\u0438\u043d\u043d\u0435\u0435 \u2014 {{ $post-&gt;author-&gt;name }} \u0432\u043d\u0443\u0442\u0440\u0438 @foreach, \u0438 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u00ab\u0437\u0430\u043f\u0440\u043e\u0441\u0430\u00bb \u0433\u043b\u0430\u0437\u0430\u043c\u0438 \u043d\u0435 \u0432\u0438\u0434\u043d\u043e.\u041a\u0430\u043a \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0431\u0435\u0437 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u2014 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b:use Illuminate\\Support\\Facades\\DB;DB::enableQueryLog();$posts = Post::latest()-&gt;take(100)-&gt;get();foreach ($posts as $post) {    $post-&gt;author-&gt;name;}dump(count(DB::getQueryLog())); \/\/ 101\u0418\u043b\u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 dev-\u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0438:\/\/ AppServiceProvider::boot()DB::listen(function ($query) {    logger()-&gt;debug($query-&gt;sql, $query-&gt;bindings);});\u0423\u0434\u043e\u0431\u043d\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u2014 Laravel Debugbar \u0438\u043b\u0438 Telescope: \u0442\u0430\u043c \u0441\u0440\u0430\u0437\u0443 \u0432\u0438\u0434\u043d\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f SQL.\u0420\u0435\u0448\u0435\u043d\u0438\u0435: eager loading$posts = Post::with(&#8216;author&#8217;)-&gt;latest()-&gt;take(100)-&gt;get();foreach ($posts as $post) {    echo $post-&gt;author-&gt;name;   \/\/ \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442}\u0422\u0435\u043f\u0435\u0440\u044c Eloquent \u0434\u0435\u043b\u0430\u0435\u0442 \u0440\u043e\u0432\u043d\u043e \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430: \u043f\u043e\u0441\u0442\u044b \u0438 SELECT * FROM users WHERE id IN (&#8230;), \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u0430\u043c \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u043f\u043e \u043c\u043e\u0434\u0435\u043b\u044f\u043c.img2-before-after\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b:\/\/ \u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0432\u044f\u0437\u0435\u0439 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0441\u0432\u044f\u0437\u0438Post::with([&#8216;author&#8217;, &#8216;comments.author&#8217;])-&gt;get();\/\/ \u0422\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u043d\u044b\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0438 (\u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u044e\u0447 id \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d!)Post::with(&#8216;author:id,name&#8217;)-&gt;get();\/\/ \u041d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u2014 \u043d\u0435 \u0433\u0440\u0443\u0437\u0438\u043c \u0441\u0430\u043c\u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438Post::withCount(&#8216;comments&#8217;)-&gt;get();   \/\/ $post-&gt;comments_count\/\/ \u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u0443\u0436\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430 \u2014 \u0434\u043e\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0441\u0432\u044f\u0437\u044c \u043e\u0434\u043d\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c$posts-&gt;load(&#8216;author&#8217;);$posts-&gt;loadMissing(&#8216;author&#8217;);        \/\/ \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0435\u0449\u0451 \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430\u041f\u043e\u0447\u0435\u043c\u0443 withCount \u043b\u0443\u0447\u0448\u0435, \u0447\u0435\u043c $post-&gt;comments-&gt;count()$post-&gt;comments-&gt;count() \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0432\u0441\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c (\u0438 \u0431\u0435\u0437 with \u2014 \u0435\u0449\u0451 \u0438 N \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432), \u0430 \u043f\u043e\u0442\u043e\u043c \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. $post-&gt;comments()-&gt;count() \u0441\u043e \u0441\u043a\u043e\u0431\u043a\u0430\u043c\u0438 \u0434\u0435\u043b\u0430\u0435\u0442 SELECT COUNT(*) \u2014 \u043d\u043e \u0442\u043e\u0436\u0435 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043d\u043e\u0432\u0430 N+1.withCount(&#8216;comments&#8217;) \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 SELECT, \u0438 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0433\u043e\u0442\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 comments_count \u0437\u0430 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441.\u041b\u043e\u0432\u0443\u0448\u043a\u0430: N+1 \u0432\u043d\u0443\u0442\u0440\u0438 accessor-\u043e\u0432 \u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432\u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u0441\u0442\u043e\u0438\u0442 with(&#8216;author&#8217;), N+1 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u0447\u0435\u0440\u0435\u0437 API Resource:class PostResource extends JsonResource{    public function toArray($request): array    {        return [            &#8216;id&#8217;       =&gt; $this-&gt;id,            &#8216;title&#8217;    =&gt; $this-&gt;title,            \/\/ \u0435\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u044c \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430 \u2014 \u0437\u0434\u0435\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441            &#8216;author&#8217;   =&gt; new UserResource($this-&gt;whenLoaded(&#8216;author&#8217;)),            &#8216;comments&#8217; =&gt; $this-&gt;whenCounted(&#8216;comments&#8217;),        ];    }}whenLoaded() \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0441\u0432\u044f\u0437\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0443\u0436\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430, \u0430 \u043d\u0435 \u0434\u0451\u0440\u0433\u0430\u0435\u0442 \u0431\u0430\u0437\u0443 \u043c\u043e\u043b\u0447\u0430.\u0417\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u043c \u043b\u0435\u043d\u0438\u0432\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u041b\u0443\u0447\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0435 \u043b\u043e\u0432\u0438\u0442\u044c N+1 \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u2014 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043f\u0430\u0434\u0430\u043b \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435. \u0421 Laravel 8.43 \u0435\u0441\u0442\u044c Model::preventLazyLoading():img3-prevent\/\/ app\/Providers\/AppServiceProvider.phpuse Illuminate\\Database\\Eloquent\\Model;public function boot(): void{    Model::preventLazyLoading(! $this-&gt;app-&gt;isProduction());}\u0422\u0435\u043f\u0435\u0440\u044c \u043b\u044e\u0431\u043e\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u043d\u0435\u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0432 dev \u0438 \u0442\u0435\u0441\u0442\u0430\u0445 \u0431\u0440\u043e\u0441\u0438\u0442 LazyLoadingViolationException. \u041d\u0430 \u043f\u0440\u043e\u0434\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u2014 \u043c\u043e\u0436\u043d\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c:Model::handleLazyLoadingViolationUsing(function ($model, $relation) {    logger()-&gt;warning(&#8216;Lazy loading&#8217;, [        &#8216;model&#8217;    =&gt; get_class($model),        &#8216;relation&#8217; =&gt; $relation,    ]);});\u0410 Model::shouldBeStrict() \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0441\u0440\u0430\u0437\u0443 \u0442\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438: \u0437\u0430\u043f\u0440\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438, \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0443 \u0438 \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043c\u043e\u043b\u0447\u0430 \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u043d\u0435\u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d\u043d\u043e\u0435 \u0434\u043b\u044f mass assignment \u043f\u043e\u043b\u0435.\u0422\u043e\u0442 \u0436\u0435 N+1 \u2014 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 Eloquent\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043d\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u0430 \u043a Laravel: \u0442\u043e\u0442 \u0436\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u00ab\u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0446\u0438\u043a\u043b\u0435\u00bb \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0447\u0438\u0441\u0442\u043e\u043c PHP \u0441 PDO, \u0432 Django ORM \u0438 \u0432 GraphQL-\u0440\u0435\u0437\u043e\u043b\u0432\u0435\u0440\u0430\u0445. \u0412 GraphQL \u0435\u0451 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0448\u0430\u044e\u0442 \u0447\u0435\u0440\u0435\u0437 DataLoader, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 id \u0438\u0437 \u0432\u0441\u0435\u0445 \u0440\u0435\u0437\u043e\u043b\u0432\u0435\u0440\u043e\u0432 \u0438 \u0434\u0435\u043b\u0430\u0435\u0442 \u043e\u0434\u0438\u043d \u0431\u0430\u0442\u0447-\u0437\u0430\u043f\u0440\u043e\u0441 \u2014 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u0435\u0441\u0442\u044c \u0432 \u0448\u043e\u0440\u0442\u0441\u0435:https:\/\/www.youtube.com\/shorts\/o92gtJKXgMc\u0427\u0435\u043a-\u043b\u0438\u0441\u0442\u0412\u0438\u0434\u0438\u0442\u0435 $model-&gt;relation \u0432\u043d\u0443\u0442\u0440\u0438 foreach\/@foreach \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435, \u0435\u0441\u0442\u044c \u043b\u0438 with().\u041d\u0443\u0436\u043d\u043e \u0447\u0438\u0441\u043b\u043e \u2014 withCount(), \u0430 \u043d\u0435 -&gt;count() \u043f\u043e \u0441\u0432\u044f\u0437\u0438.\u0412 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445 \u2014 whenLoaded() \/ whenCounted().\u0412 AppServiceProvider \u2014 Model::preventLazyLoading(! app()-&gt;isProduction()).\u0412 \u0442\u0435\u0441\u0442\u0430\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 DB::getQueryLog().\u0415\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c Eloquent, \u0441\u0432\u044f\u0437\u0438, \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0439 Laravel \u2014 \u0435\u0441\u0442\u044c \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u043a\u0443\u0440\u0441 \u0441 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u043e\u0439: Laravel \u043d\u0430 \u0421\u0443\u0431\u043a\u043e\u043d\u0442\u043e.\u0410\u043a\u0430\u0434\u0435\u043c\u0438\u0438.\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 https:\/\/habr.com\/ru\/articles\/1101990\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-501110","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/501110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=501110"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/501110\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=501110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=501110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=501110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}