Faster Eloquent: Avoid Accessors with Foreach
ฝัง
- เผยแพร่เมื่อ 9 ก.พ. 2025
- In this video, step-by-step we will optimize a page loading from 30+ seconds to 0.2 seconds, by avoiding a foreach loop with Eloquent Attributes.
Spatie Ray: spatie.be/prod...
My 20 Laravel Eloquent Tips and Tricks: laravel-news.c...
- - - -
Support the channel by checking out our products:
Enroll in my Laravel courses: laraveldaily.t...
Try our Laravel QuickAdminPanel: bit.ly/quickad...
Purchase my Livewire Kit: livewirekit.com
View Laravel Code Examples: laravelexample...
Subscribe to my weekly newsletter: bit.ly/laravel-...
I believe this is by far the best laravel channel on TH-cam. Correct me if I'm wrong.
Possibly the single most useful tip I've seen this year. Thanks Povilas.
Keep dropping them gems. Thank you
All of this spesific talk was music for my ears. I love tips on performance tuning, not only performance wise, it also helps you to be a better architect or engineer by following patterns AND improving cognative skills for future projects.
Watching your videos has made me an optimization freak tbh. And I'm not complaining 😊
This is a great video, I love learning performance stuff like this. It's the nuts and bolts of a tool that people don't talk about often enough. Please make more videos like this!
Nice that you elaborate your thinking process
Awesome video👏👏 this plus the laravel-news article feels like a level up for me... So much actionable knowledge!!!
New video = instant like!
Thank you!
Povilas GOOD JOB, getting better day after day, Thanks for your incredible amount of work!
thank you! you deserve more views !
Great and very helpful all your laravel video thanks 👍
That's a huge optimization ! Thanks !!
It was really helpful. thanks
WOOW very intersting , we need more of those videos plz ( explain for us the eager VS normal ) loading data
Nice! Also, like you said at the end about caching, sometimes thats the best solution. You can use the existing logic and set an identity column inside of the User updating model event, then you bypass the need for an accessor.
Su canal es el mejor, he aprendido mucho de usted ¡muchas gracias! buen contenido
Always on point. Thank you!
Great Video, thanks
I'm sure there's a perfectly reasonable sql query that returns the same results. This is a perfect example of when NOT to use Eloquent.
I completely agree with you. I recently found out when using "with('relation')", Eloquent will not perform an inner or left join, it will make a second "select ... where parent_relation in (all ids of parent_relation)", which for me was counter intuitive at first, but in the end to be able to have such a powerful chainable language you need to make some tradeoffs I guess. It still does the job pretty well to have a readable code!
My first thought was to make a view in the DB lmao
A very good lesson!
finally i got what i looking for ( thanks)😊
How can I add this video to some permanent list ? This is beyond excellent. Thank you so much Povilas!
Thank you!
Great approach! Thx
Thank you 🙏🏻
Amazing!
Thanks 4 tutorial
Once again, a great and educational video! Thanks! However, a small suggestion if you wish to push performance and be more strict with how the app deals with variables (less unexpected behaviour): use identical comparison (===) where possible. I noticed that for several comparisons where it should always be a comparison between two integers, a lossy equation (==) is used.
Good video.
The most erreur is : use trust orm and don't know how bdd work
awesome ❤
Thanks
It is so cool, thank you for this video!:)
very interesting. thx
Thank you so much for this video. Fantastic example how Laravel devs abuse Models nowdays.
whats baffling me is he does select id from users but in attr he uses $this->name which i assume is an attr of that table.. :/ this name will always be null/empty since its not selected
He discussed and solved that in the video :/
When I have to choose between a method on eloquent and an accessor, i check if there is any query launched to db, if yes, then it is definetly not an accessor as accessors are loaded on model load, while methods don't.
Which server are you using in mac for running laravel applications? Like xampp or any other?
Laravel valet
I have an issue, i don't know if its laravel or db issues, but if idle for a certain period of time and tried to retrieve data on the database I got like 1.5k to 3k ms , but on 3rd or 4th call its like on for 400ms, same records it was same records, and I'm using mongodb.
nuostabus, plojimai!
Nice work o/
That's really important topic to follow in development. If you have the right tool then task seems more interesting to do.
I haven't found the name of this debugging tool in description please share the link.
Composer require barryvdh/laravel-debugbar, also Laravel telescope and Spatie Rsy - those are the tools
So basically it is issues with queries itself, not with accessor. Is it? If I'm using just get modify date format will it perform bad too?
Excellent. Could You please link the Ray setup video? If I'm not mistaken You have a nice presentation.
th-cam.com/video/n4pMxyAXeqY/w-d-xo.html
@@LaravelDaily Thanks
Great video! Sometimes there's no reason not to do what the DB is designed to do. On the other hand, there are situation like the following: suppose you have a Posts/Comments project, and you want to show all the comments of a user, but visually divided by post. This is not a "SQL-grouping", cause you don't need to use some aggregate function on the data, but just to have them organized by post. On your opinion, what's a good way to handle these data? I am going with a foreach and an associative array, but doesn't feel right...
Why is it not SQL grouping? When doing the query, you group by post_id, and then in the Blade do foreach ($post) -> foreach ($post->comments) etc.
Because when you group by post_id (in SQL syntax) you aggregate the results using count, sum, etc. That's what I mean with "SQL grouping". Are you referring to using the groupBy function of Laravel?
Yes I thought so, actually thinking about it, it's best to group in collection then, so something like ->get()->keyBy(post_id) or something, don't remember the exact collection method.
@@LaravelDaily Didn't know about the keyBy method. I'll dive into it, thanks!
Can you please make a video on recursive function when category needs to be display in hierarchy? Because it causes performance problem
This video should help: th-cam.com/video/5s-_SnVl-1g/w-d-xo.html
Why don't you use laravel ide helper? My perfectionist eyes are bleeding seeing those inspector warnings. But besides aesthetic aspect, ide helper really improves the quality of code by avoiding typos and development speed by autocomplete. I always try to make each php file having green inspector mark, thus, especially if you use type hints, return types and declare strict, green mark says that everything here is correct and you need to pay attention only to the logic
why are comments being deleted?
I haven't deleted any comments, except for obvious spam bots
@@LaravelDaily ok must be some yt issue
The approach of original code was breaking single responsibility principle and was wrong from the very beginning
Thing is... 700ms is ridiculous for a "production" ready framework
I am guilty
not a good example at all. the code and schema should be refactored.
Names of this metod is a straight from hell 😅 is_ghost - rally?!?
very good thanks