#N+1
15 messages · Page 1 of 1 (latest)
createdGroups is the name of one of my relationships between the User and Group models. But I don't understand why I didn't need to load it with
Auth::user()->load(“createdGroups”)
Here's the explanation: https://github.com/laravel/framework/pull/37503
If I understand correctly, I don't have any exceptions because I only have one entry linked to the Group model?
It's a little confusing.
It’ll perform one query, just like when using with, so there is no N+1 here. It’s a N+1 if you start doing loops and access relations that weren’t loaded
OK
I'm reopening this post because there's something I still find very abstract. When is it not necessary to do eager loading to avoid n + 1 errors? Because in another project, I created a one-to-many/many-to-one relationship between the users table and another table that I called tests. A user can be associated with several test values, but a test can only be associated with a single user.
However, in my route, if I don't do Auth::user->load(‘tests’), it returns a blank page.
I did exactly the same thing as in the project mentioned earlier, where it works fine.
I should point out that in both cases I am using Inertia React.
my web.php
<?php
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
Route::get('/', function () {
return Inertia::render('welcome');
})->name('home');
my welcome.tsx
import { Head, usePage } from '@inertiajs/react';
import { SharedData } from '@/types';
export default function Welcome() {
const {auth} = usePage<SharedData>().props
return (
<>
<Head title="Welcome">
<link rel="preconnect" href="https://fonts.bunny.net" />
<link href="https://fonts.bunny.net/css?family=instrument-sans:400,500,600" rel="stylesheet" />
</Head>
<div className={'space-y-5'}>{auth.user && auth.user.tests.length}</div>
</>
);
}
You’re just not sending that data to the frontend? 🤷
Feel like you’re mixing up a lot of things, n+1 isn’t a strategy or whatever, it’s a problem one can run into.
Like Robert mentioned, where would your frontend get the data from when you're only using the referenced(auth user) in the frontend?
You're probably not serializing models yourself, so just sending the models to the frontend. That means it would take whatever properties the model has and send that to the frontend. So loading a relation would set that property and also serialize it. If the relation isn't loaded, which is default because why would Laravel load a relation for a model if you're not asking for it, then that also wouldn't end up on the frontend.
I've thought about it carefully, and I think that in my previous project, I didn't need to use load because I called the relationship directly in my controller function that returns the view, unlike in this current project where I only execute it in the view, and in that case I think eager loading is necessary. I know that n+1 is an error that can occur, but my goal is to do everything I can to avoid it altogether. However, I feel that eager loading to prevent this is not always necessary when using relationships, depending on where they are called. I'm not sure if that's very clear.
Let me ask you, do you know what ->load(...) and ->relation do? Do you know when a N+1 issue can occur, like, do you know what it means?
if you're not sure what it is as indicated above then leave this in your app service provider and you'll soon get an error locally (and it will be clear) ```php
Model::shouldBeStrict(! app()->isProduction());
I don't know what ->relation does because I've never heard of it, but I know that ->load(...) allows you to load relationships in order to avoid n+1 errors.
If I understand correctly, n+1 errors are, for example:
- If we have 100 users and we want to retrieve all the content from a table associated with each of them (e.g., Articles), if we don't load the relationship, the query will be executed 100 times (because we have 100 users), so we will have a total of 101 queries executed instead of 2.
Translated with DeepL.com (free version)