Eloquent Performance: TOP 3 Mistakes Developers Make

  Рет қаралды 48,011

Laravel Daily

Laravel Daily

Күн бұрын

I've talked a lot about Eloquent on this channel but decided to pick the three most common mistakes related to performance.
- - - - -
Support the channel by checking out our products:
- Enroll in my Laravel courses: laraveldaily.teachable.com
- Try our Laravel QuickAdminPanel: bit.ly/quickadminpanel
- Purchase my Livewire Kit: livewirekit.com
- View Laravel Code Examples: laravelexamples.com
- Subscribe to my weekly newsletter: bit.ly/laravel-newsletter

Пікірлер: 111
@Stoney_Eagle
@Stoney_Eagle 2 жыл бұрын
When a TV show has casts, crews, posters, backdrops, translations, trailers and seasons, seasons has translations and episodes, episodes has translations and a video file and video file has a progress you learn quick to do all these eager loadings on the database level, not to mention using the right indexes on the keys your relations uses. That saves 2 to 5 minutes and 500mb on the worst case scenario 😅
@Bevallalom
@Bevallalom 2 жыл бұрын
In certain cases it's better to use raw sql then query builder. Also proper db indexing is important factor.
@ihorchyshkala6325
@ihorchyshkala6325 2 жыл бұрын
My rule:
@PauloRicardo-dk6yf
@PauloRicardo-dk6yf 2 жыл бұрын
I am Brazilian and like very much your videos.
@maxpower7735
@maxpower7735 2 жыл бұрын
When using eager loading you may need to check if the data was loaded. For that Laravel has the function "relationLoaded".
@belce1982
@belce1982 2 жыл бұрын
Thanks as always. Saved to watch later. Keep the hard work!
@corbitaltechnologies
@corbitaltechnologies 2 жыл бұрын
Very specific and detail example. Very helpful.
@sodomousprime
@sodomousprime 2 жыл бұрын
The last one is like when you have a HTML select input, where you only need the "id" and the "title" rather than loading all of the rest of the data. Good video, cheers.
@ClCfe
@ClCfe 2 жыл бұрын
Model::pluck('title', 'id'); Be careful, do not do Model::get()->pluck('title', 'id'); As it does a Select * and then call pluck on the collection
@nadjimmalade2847
@nadjimmalade2847 2 жыл бұрын
Exactlly i see a lot of people doing that.
@exikoz
@exikoz 2 жыл бұрын
Such great content! Thank you very much!!!!
@shahosanandaji9894
@shahosanandaji9894 2 жыл бұрын
Thanks a lot again Povilas for making these videos.
@anastasiospapadopoulos4612
@anastasiospapadopoulos4612 2 жыл бұрын
Thanks alot for these advices!
@user-ej4dm2xq6q
@user-ej4dm2xq6q 2 жыл бұрын
Very helpful. Thank you.
@abdulbasitsalah2918
@abdulbasitsalah2918 2 жыл бұрын
i like how you categorize your video with the related topic for example in this video you prefix title with "Eloquent Performance:" it will be so good when someone search for performance in channel 👏👏
@mahdihassannejad6157
@mahdihassannejad6157 2 жыл бұрын
I should thank you a lot Povilas, I always start my day by watching one of your videos, And it would opens my mind along the day.
@nurudeenqoyyumtimilehin5475
@nurudeenqoyyumtimilehin5475 2 жыл бұрын
Informative, thanks for sharing.
@daleryanaldover6545
@daleryanaldover6545 2 жыл бұрын
Another way to explain $user->post->count() is that when we load a model either with relationship or not the returned data is a collection instance. And in the collection documentation we have ->count() method that counts data from the returned collection and not from databse.
@user-rm4zo1mg5o
@user-rm4zo1mg5o 2 жыл бұрын
One more important thing, it's very easy to use model $with variable to eager load relationship. But in much cases this data isn't used for example in model index. In that cases you can see how the performance decrease, specially with polymorphic relationship. It's better to retrieve relationships only for show and edit methods, where they will be really used, but not always through the model $with.
@tim.bogdanov
@tim.bogdanov Жыл бұрын
Whoa, this is huge, I've been using with incorecctly. Thank you
@flyer0022f
@flyer0022f 2 жыл бұрын
This was helpful, thank you
@naturallifenow
@naturallifenow 2 жыл бұрын
Great! I made the 3rd mistake too much when I made PDF reports that contained more than 10,000 records. Instead of creating a ReportResource with only the fields needed for the report, I used a Resource that returned all the fields in the table. For many records, it makes a total difference in the total size of json that the frontend needs to download from the backend.
@rehmanghani6726
@rehmanghani6726 Жыл бұрын
Nice video Povilos 👍 Keep up the good work.
@tukurhamid4703
@tukurhamid4703 Жыл бұрын
Thank you very much sir
@jazzlevit
@jazzlevit 2 жыл бұрын
Thanks, quite useful
@arnaudbakyono461
@arnaudbakyono461 10 ай бұрын
Wow, really good tips
@muaazhammad4446
@muaazhammad4446 2 жыл бұрын
thanks for these tips
@masterpreshy9614
@masterpreshy9614 2 жыл бұрын
Thank you.
@afjalhusain302
@afjalhusain302 Жыл бұрын
Thanks sir
@RehmanKhan-zv2rl
@RehmanKhan-zv2rl 2 жыл бұрын
Great, I was always confuse to use relation with '( )' or without '( )', and also with eager loading, i have cleared the difference between that but can you please define more about eager loading and lazy loading and stuff like that, It would be really helpful. Thanks
@YehudaKatz1
@YehudaKatz1 2 жыл бұрын
I do filtering in PHP if whether the user can see an object is based on some permissions. For example, different users in a store have different payment methods available. I can't think of a simple way to filter that in the database. In PHP, you can use Collection::filter() to check if $user->can().
@lahcenfahmi5361
@lahcenfahmi5361 2 жыл бұрын
Thanks
@GergelyCsermely
@GergelyCsermely 2 жыл бұрын
Thanks.
@nabeelyousafpasha
@nabeelyousafpasha 2 жыл бұрын
Do you mind sharing those examples where PHP manipulation was seen more speedy than MySQL? Please share your experiences. Eager to learn more from you. Respect from Pakistan.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
I don't remember them, to be honest, it was a long time ago, I just remember the surprised feeling about it.
@jing4625
@jing4625 2 жыл бұрын
Thx
@mouctechy
@mouctechy Жыл бұрын
Thank you sir for sharing it. I would like to know which front end framework do you suggest for a mid beginner of Laravel ? Live wire; vue, reactjs, jet-stream ,…..
@RANJEETKUMAR-wz4dg
@RANJEETKUMAR-wz4dg 2 жыл бұрын
thanks
@sourovahmad4053
@sourovahmad4053 2 жыл бұрын
Great as alwayes
@user-xn4jg2fc5n
@user-xn4jg2fc5n 2 жыл бұрын
Spasibo!
@raphaeld7308
@raphaeld7308 2 жыл бұрын
you look like Mauricio Pochettino
@henriquefriedrich5960
@henriquefriedrich5960 2 жыл бұрын
Indeed :)
@heyyy4987
@heyyy4987 2 жыл бұрын
Awesome
@mukhtarhussain2659
@mukhtarhussain2659 2 жыл бұрын
amaizng
@AbdelrahmanEl.Monshed
@AbdelrahmanEl.Monshed 2 жыл бұрын
good
@pacykarz2009
@pacykarz2009 2 жыл бұрын
What is the better: $user->posts->count() or count($user->posts) ?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Probably same
@iliyasmsc
@iliyasmsc 2 жыл бұрын
Good,but how to get the user avatar in or other map fields in this query.
@alislim6201
@alislim6201 2 жыл бұрын
am using join and leftjoin function in some cases instead of laravel relationship i find that way is better for me do you have any suggestions ?
@rizkiakbar4866
@rizkiakbar4866 2 жыл бұрын
I new on laravel thanks for your helpfull video. How do u can view that's laravel process windows down there ?. I had no idea how to do that.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Composer require barryvdh/laravel-debugbar
@rizkiakbar4866
@rizkiakbar4866 2 жыл бұрын
@@LaravelDaily ooh it was composer library, I thought it was some new laravel feature. thanks
2 жыл бұрын
Hi Povilas, nice video. Which is your current stack for php/laravel development locally? I am using docker locally, but your video it seems very faster.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Laravel valet on my MacBook
2 жыл бұрын
@@LaravelDaily Tks for the answer. It looks very good. Docker became lazy because of the sync of files between container/local env even using :delegated
@jomonjohnson735
@jomonjohnson735 2 жыл бұрын
could you review Encore\Admin\Layout\Content approach for rendering views ( from z-song/laravel-admin package)
@LaravelDaily
@LaravelDaily 2 жыл бұрын
I don't think this individual case would be very useful for the audience.
@jacquesmatike9289
@jacquesmatike9289 2 жыл бұрын
For the last case, I have a similar problem. In my database, I have almost 13.000 records of users. I have to process them all to get stats of users by checking an attribute value (0, 1, 2, 3). Loading data from database is fast, but process them in the foreach takes 40s. And obviously only that attribute and ID are loaded. Is there a way to do that operation for minimum time?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Send me the code to povilas@laraveldaily.com maybe it's worth shooting a video about.
@SanderCokart
@SanderCokart 2 жыл бұрын
any alternative to debugbar for laravel + SPA configs?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Laravel Telescope
@abelenocrodriguez
@abelenocrodriguez 2 жыл бұрын
How would you do an API to debug this? Please, it would be very interesting to attack this type of problem, basically because my stack in more than 200 projects is always Vue.js and Laravel API
@ashishdake1197
@ashishdake1197 2 жыл бұрын
@LaravelDaily I liked your videos much. Rather I say, I am learning Laravel by watching your videos only. I have a doubt. At 1:30, 2:25 or 5:05 you are showing us DB queries count and all DB queries. How can I see it in my browser. Is there any specific software or chrome extension for it? Also, please share list of all extensions and softwares that you use while development.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
It's Laravel Debugbar
@suhpc82
@suhpc82 2 жыл бұрын
When you query only a select few columns of a relationship, should we do that within the with() call, or in the get() call?
@gabrielilochi7840
@gabrielilochi7840 2 жыл бұрын
It should be within with() For example retrieving comment relation with required fields; with('comment:id,user_id,text,created_at')
@suhpc82
@suhpc82 2 жыл бұрын
@@gabrielilochi7840 Thanks mate 👍
@SanderCokart
@SanderCokart 2 жыл бұрын
what is the benefit of compact() ?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Shorter syntax for array
@zafarbekbadalboyev9689
@zafarbekbadalboyev9689 2 жыл бұрын
Hi sir, thanks for tips. Can you tell us, is there any tool for REST API debugging for laravel like Laravel Debugbar?
@anthoniG
@anthoniG 2 жыл бұрын
I think you can use Laravel Telescope. Don't quote me on that though as I am still relatively new to the whole Laravel experience.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Telescope and Ray, a few videos about them: kzbin.infosearch?query=debug%20api
@vitorfontoura5417
@vitorfontoura5417 2 жыл бұрын
You could use itsgoingd/clockwork. It has a similar feature to debugbar to add a footer with some request information, and in my opinion is a bit simpler than Telescope.
@darkmode404
@darkmode404 2 жыл бұрын
I think you can still use the Debugbar but you have to use the backend url with /api/posts like localhost: 8000/api/posts
@abuhenamustafamohammedsaha4386
@abuhenamustafamohammedsaha4386 2 жыл бұрын
Using Withcount takes 5.15 ms whereas with eager loading it takes only 2.1 ms which one is better in terms of performance
@noddychoi
@noddychoi 2 жыл бұрын
What should I do if I want to join across 4 four tables? The "hasManyThrough" seems only support 3 tables.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Do manual ->join()
@kuyapols4037
@kuyapols4037 2 жыл бұрын
May i know what is the package name or extension you use like inspect elements for laravel?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Laravel Debugbar
@kuyapols4037
@kuyapols4037 2 жыл бұрын
@@LaravelDaily Thank you for the noticed :D
@kevinpallado9638
@kevinpallado9638 2 жыл бұрын
What console log are you using for this?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Laravel Debugbar
@kevinpallado9638
@kevinpallado9638 2 жыл бұрын
@@LaravelDaily can I use it when I only use laravel as an api endpoint?
@bugsbunny4353
@bugsbunny4353 2 жыл бұрын
Hi sir Can u make a video on upload large CSV in db fast using queue I worked on it its working fine but due to my server limitation sometimes queue fails
@ClCfe
@ClCfe 2 жыл бұрын
Fastest: use mysql LOAD DATA INFILE Or laravel lazy collections + chunked inserts
@bugsbunny4353
@bugsbunny4353 2 жыл бұрын
@@ClCfe i am already using laravel jobs by breaking large csv into small files chunks then using jobs to save in background using particular user id
@amitsolanki9363
@amitsolanki9363 5 ай бұрын
In laravel debugger What does the model indicate? Like models 300, like models 120
@LaravelDaily
@LaravelDaily 5 ай бұрын
The amount of Laravel eloquent models saved to RAM during this request.
@amitsolanki9363
@amitsolanki9363 5 ай бұрын
How can I reduce amount of Laravel eloquent models
@KevinYobeth
@KevinYobeth 2 жыл бұрын
How do we know if we create an inefficient query?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Hard to answer in a KZbin comment. Emmm, you measure it against other possible queries?
@vikymuhamad4226
@vikymuhamad4226 2 жыл бұрын
hi sir i still not understand about ur relasi using withCount(posts) and u get data in index using $user->post_count , and ur relasi name is posts , do u can tell me about $user->post_count coz ur name relasi is posts not post_count . thanks
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Laravel docs: laravel.com/docs/8.x/eloquent-relationships#counting-related-models
@shaungbhone8368
@shaungbhone8368 2 жыл бұрын
What happens when I don't solve N+1 problems? Someone helps. Thanks
@salisu14
@salisu14 2 жыл бұрын
App performance will be drastically reduced...
@codingsense
@codingsense 2 жыл бұрын
Good video thanks , can you please make a video on how to design a database digram I struggle a lot with it Please like this comment for him to see this
@LaravelDaily
@LaravelDaily 2 жыл бұрын
I have a full course: laraveldaily.teachable.com/p/how-to-structure-database-in-laravel Also, an old video: kzbin.info/www/bejne/iJOudox6qdB0gKs
@mmramadan496
@mmramadan496 2 жыл бұрын
Excuse me, sir I have seen two methods in the Eloquent part are `fresh` and `refresh` BUT I do NOT know what is the benefit of these methods So, can you please explain more about these methods?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Where exactly have you seen them, in what context? Can you give me the example/link?
@mmramadan496
@mmramadan496 2 жыл бұрын
@@LaravelDaily I can NOT send the link because every time I send a link it deleted So, search for `Eloquent: Getting Started` in the docs then you'll find the `Refreshing Models` title
@MirzaBilal1992
@MirzaBilal1992 2 жыл бұрын
How to use laravel debugger ? Can you please assist me in this regard?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Google "laravel Debugbar" and read its documentation
@MirzaBilal1992
@MirzaBilal1992 2 жыл бұрын
@@LaravelDaily Thanks a lot for your kind reply. I have installed successfully.
@hariomnagar5606
@hariomnagar5606 2 жыл бұрын
Who can I test these kind of mistakes for API request or controller
@LaravelDaily
@LaravelDaily 2 жыл бұрын
With Laravel Telescope
@hariomnagar5606
@hariomnagar5606 2 жыл бұрын
@@LaravelDaily Thanks I will try Telescope
@chiragkumar6322
@chiragkumar6322 2 жыл бұрын
How to secure .evn file in laravel.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Don't push it to GitHub
@chiragkumar6322
@chiragkumar6322 2 жыл бұрын
@@LaravelDaily Thank you sir for reply. but actually my issue it is. I have make a Laravel 5.8 project In which many API are make for android APP. And one user find a bug his using our app and get some error. It shows the code of the .evn file. so how to secure .evn file to get any error.
@sumanthapa4670
@sumanthapa4670 2 жыл бұрын
Sir, I need to load 10 different kind from database news table based on their category which would be faste method 1:- $news_cat_1 = News::where('cat_id','1')->get(); $news_cat_2 = News::where('cat_id','2')->get(); . . . $news_cat_10 = News::where('cat_id','10')->get(); Method 2: $allNews = News::all(); $news_cat_1 = $allNews->where('cat_id','1')->get(); $news_cat_2 = $allNews->where('cat_id','2')->get(); . . . . $news_cat_10 = $allNews->where('cat_id','10')->get();
Eloquent Performance Challenge: My Solution Version
9:57
Laravel Daily
Рет қаралды 19 М.
SOLID Principles in Laravel: 5 Examples (+ New Course!)
21:07
Laravel Daily
Рет қаралды 71 М.
Khóa ly biệt
01:00
Đào Nguyễn Ánh - Hữu Hưng
Рет қаралды 21 МЛН
Must-have gadget for every toilet! 🤩 #gadget
00:27
GiGaZoom
Рет қаралды 12 МЛН
Laravel Security: Top 7 Mistakes Developers Make
11:16
Laravel Daily
Рет қаралды 81 М.
18 Laravel/PHP Tips in 10 Minutes: June 2024
10:41
Laravel Daily
Рет қаралды 5 М.
Exceptions in Laravel: Why/How to Use and Create Your Own
12:18
Laravel Daily
Рет қаралды 86 М.
6 Tips To Get More Dev Job Opportunities
5:19
Laravel Daily
Рет қаралды 2,1 М.
Faster Eloquent: Avoid Accessors with Foreach
9:35
Laravel Daily
Рет қаралды 52 М.
Top 5 Laravel "Bad Practices" (My Opinion)
10:32
Laravel Daily
Рет қаралды 18 М.
Why is Laravel NOT used in Big Development Projects?
11:53
Stefan Mischook
Рет қаралды 171 М.
9 Tips for Shorter Laravel Code
10:16
Laravel Daily
Рет қаралды 61 М.
Laravel vs React
9:40
Aaron Francis
Рет қаралды 46 М.
Невероятный талант😮
0:20
Лайтшортс
Рет қаралды 10 МЛН
My little bro is funny😁  @artur-boy
0:18
Andrey Grechka
Рет қаралды 13 МЛН
Покатались на сноубордах😂
0:10
FERMACHI
Рет қаралды 2,1 МЛН
LAVOU TÁ NOVA!
0:11
DAIANE VARGAS
Рет қаралды 45 МЛН
easy game filter 😎
0:12
Nandito Creative
Рет қаралды 6 МЛН