Note that this code should be executed only on your local machine or testing/staging servers, live users on production servers should not see this message, cause that would be a security issue. This will show you the exact "dangerous" code that you may want to fix and optimize. Now, if you launch any page that contains an N+1 Query problem, you will see an error page, something like this: In our case, it will be books that will be listed with their covers. Laravel-medialibrary package uses polymorphic relationships between the "media" DB table and your model. Don't get me wrong: the package itself is awesome and I don't want to show it as a flaw in the package, but rather as an example of how important it is to debug what is happening under the hood. Let me show you an example with a very popular spatie/laravel-medialibrary package. You can run into unexpected N+1 queries if you're not careful. Laravel has a great ecosystem of packages, but sometimes it's dangerous to use their features "blindly". In your current case, you may avoid the N+1 query, but in the future, someone else may not think about it. In other words, Accessor is supposed to be a reusable method for formatting data, so you're not in control of when/how it will be reused. Because an accessor is usually used when showing the data, and in the future, someone else may use this accessor in some other Blade file, and you will not be in control of what that Controller looks like. But in this case, my overall advice is avoid using relationships in accessors. Yes, we can solve it by eager loading the books in the Controller. So, we have the list of authors loaded, and again, look what Debugbar shows: You can also use the "older" syntax of defining the method getIsActiveAttribute(), it will also work in the latest Laravel version. Notice: this is a new syntax of Laravel accessors, adopted in Laravel 9. Use Factory classes and then generate 10,000+ records for books/authors and other models: Preferably, a lot of data, so you would see how your project performs "in real life" if you imagine it growing successfully in the future months or years. In addition, I also recommend having seeder classes that would generate some fake data. So, using Debugbar is only one part of the advice. But this tool by itself will not show the obvious problems until you have more data on the pages. Of course, I advise you to use Laravel Debugbar on all your projects. Security Notice: make sure that when your project goes live, you have APP_DEBUG=false on that server, otherwise regular users of your website will see the debugbar and your database queries, which is a huge security issue. env variable APP_DEBUG=true, which is a default value for local environments. You just need to enable debugging with the. This one can be taken directly from the official Laravel documentation:ġ composer require barryvdh/laravel-debugbar -devĪnd that's it, it will show the bottom bar on all the pages. But before we get to the solutions, let's discuss the problems. To solve it, we need to query the related records upfront, and Eloquent allows us to do that easily, with so-called eager loading. So, N queries, plus the record itself, total N+1. Why is it called "N+1"? Because, in the Eloquent case, it queries ONE row from the database, and then performs one more query for EACH related record. This is not only an Eloquent, or even Laravel, problem: it's well-known in the dev industry. It happens because Eloquent allows developers to write a readable syntax with models, without digging deeper into what "magic" is happening under the hood. In short, it's when Laravel code runs too many database queries. In this article, I will show a few different examples of what to watch out for, including the cases when the problem is "hidden" in unexpected places in the code. A big part of that is a so-called "N+1 Query Problem". Eloquent performance is typically the main reason for slow Laravel projects.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |