Laravel Code Review: Optimize Queries and Other Performance

  Рет қаралды 11,634

Laravel Daily

Laravel Daily

Күн бұрын

Today I'm reviewing the Artisan command code that I received via email.
Related video. Laravel: Be Careful with whereHas Performance in Eloquent • Laravel: Be Careful wi...
- - - - -
Support the channel by checking out my products:
- My Laravel courses: laraveldaily.c...
- Laravel QuickAdminPanel: quickadminpane...
- Livewire Kit Components: livewirekit.com
- - - - -
Other places to follow:
- My weekly Laravel newsletter: us11.campaign-...
- My personal Twitter: / povilaskorop

Пікірлер: 32
@longingheart77
@longingheart77 Жыл бұрын
I believe you can check if date is during the weekend via Carbon instance method
@jcc5018
@jcc5018 Жыл бұрын
so your part about caching the date I would never have thought of. Mainly cause I haven't thought about caching at all as Im still trying the get the other parts figured out. But I always assumed I would eventually just set up caching for whole pages that dont have to change all that often. I never really considered caching small pieces of data like this for optimization. So if you dont already have a video on this, perhaps you could make one that could explain different scenarios in which various levels of caching would help optimize an application.
@LaravelDaily
@LaravelDaily Жыл бұрын
Searched "caching" on the channel for you: www.youtube.com/@LaravelDaily/search?query=caching
@jcc5018
@jcc5018 Жыл бұрын
@@LaravelDaily Thanks, its not as easy to search individual channels on mobile, but I also am not really in need of the topic personally at this time, Just wanted to provide a suggestion since you have asked for video ideas before, and I know you cover topics in different ways at times as there are many different ways topics can be implemented. This video prompted a question I thought may be worth diving deeper into, so I offered the suggestion. But i'll "cache" the response for future reference. I've got to go figure out how to use google ad manager
@kingstalker
@kingstalker Жыл бұрын
Good quistion
@erikguillen6599
@erikguillen6599 11 ай бұрын
just learned bout it too
@nsejanvier
@nsejanvier 6 ай бұрын
​@@LaravelDaily I want laravel 11 VS Flutterwave
@ZyronZA
@ZyronZA Жыл бұрын
Why is this doing DB access and business logic in one function? How will you write tests for that command if you cannot mock the Holiday:: and User:: object, or even the business logic? What's the purpose of Day::sunday->name if it already has the name "sunday"?
@LaravelDaily
@LaravelDaily Жыл бұрын
Valid points, making this artisan command testable would mean a strong refactoring. Maybe you would want to perform it? I would gladly shoot a video about it.
@Theo-bz8kk
@Theo-bz8kk Жыл бұрын
You can still make feature tests for this command.
@ZyronZA
@ZyronZA Жыл бұрын
@@LaravelDaily Hey! I don't see the purpose a command having a test written for it as it's just to coordinate operations. My thinking here is the business logic should be in its own class and injected into the command constructor. Then the Holiday:: + User:: object are injected into the business logic class. A test is then written for the business logic class to lock down its functionality.
@junjunghasudungan1905
@junjunghasudungan1905 11 ай бұрын
Hello sir, User()->query() The Meaning is instence to model user for elequent? What a benefit to used? Thanks for education us with this video
@LaravelDaily
@LaravelDaily 11 ай бұрын
This video is the answer: kzbin.info/www/bejne/r6fMh5SObJKqipY
@bumblebity2902
@bumblebity2902 Жыл бұрын
Is it big "NO" to use DB queries in helper functions or should I cache daily for example to convert currency?
@LaravelDaily
@LaravelDaily Жыл бұрын
It depends on the situation, there's no single answer.
@syracuse4612
@syracuse4612 Жыл бұрын
Nice🥰
@codewithrex
@codewithrex 7 ай бұрын
In my opinion you could also use ->lazy() instead of ->get() when querying the users - it sacrifices a runs a little bit slower for huge memory savings.
@ryiad2010
@ryiad2010 2 ай бұрын
This is very useful, thank you
@DeepStreamBits
@DeepStreamBits Жыл бұрын
I only use model query if I actually want to get the model, otherwise I'm just doing a DB::table('***') query
@anesu6184
@anesu6184 Жыл бұрын
The only addition I would do is add pagination logic so that if I have many employees the command will continue working without any changes.
@jmyers2896
@jmyers2896 Жыл бұрын
It would be good to run explain on the raw sql queries and checking for any full table reads. Then add indexes as needed to optimize those queries.
@timothybelekollie6461
@timothybelekollie6461 Жыл бұрын
this is great sir, thanks for always enlightening us.
@TrueWebDev
@TrueWebDev Жыл бұрын
Hi, here could be a refactoring issue, "attendances" is HasToMany relation, so join can produce more than one user entity and we will get extra items in result $data array, this can cause data inconsistency or something else.
@LaravelDaily
@LaravelDaily Жыл бұрын
Technically, you're right. But I guess those extra items could be identified and filtered out with where() conditions.
@BinaryKitten
@BinaryKitten Жыл бұрын
With the query change, you'll also need to update the data side as shift end_at won't be on a relation.
@LaravelDaily
@LaravelDaily Жыл бұрын
Yes great point
@bc0078
@bc0078 Жыл бұрын
Very nice but shouldn't weekend be sunday and saturday?
@LaravelDaily
@LaravelDaily Жыл бұрын
Not in all countries of the world
@mohammedattar3628
@mohammedattar3628 Жыл бұрын
i need to email your sir
@-slash-7772
@-slash-7772 Жыл бұрын
Hi, I have a question about deleting "second degree relationships." Lets say I have a grandparent->children->grandchildren and when I delete a grandparent, my goal is to delete its children and grandchildren. I'm not sure if there is a good, standard solution, but here is ChatGPT's working solution: // Grandparent.php (the function is utilized in the GrandparentController.php when deleting) public function deleteWithRelated() { // Delete grandchildren $this->grandchildren()->each(function ($grandchild) { $grandchild->delete(); }); // Delete children $this->children()->each(function ($child) { // Delete children $child->delete(); }); // Finally, delete the grandparent $this->delete(); }
@LaravelDaily
@LaravelDaily Жыл бұрын
I would personally do it on the database level, in migrations: $table->foreignId()->constrained()->cascadeOnDelete(); But if you don't have that in the database, then yeah, I guess ChatGPT solution is ok.
@LaravelDaily
@LaravelDaily Жыл бұрын
Following up, I've just published this tutorial. Laravel Parent Children and Grandchildren Delete: Three Options laraveldaily.com/post/laravel-parent-children-grandchildren-delete-records
Junior Code Review: Laravel Routes, Middleware, Validation and more
19:57
Faster Eloquent: Avoid Accessors with Foreach
9:35
Laravel Daily
Рет қаралды 53 М.
OYUNCAK MİKROFON İLE TRAFİK LAMBASINI DEĞİŞTİRDİ 😱
00:17
Melih Taşçı
Рет қаралды 12 МЛН
The selfish The Joker was taught a lesson by Officer Rabbit. #funny #supersiblings
00:12
Funny superhero siblings
Рет қаралды 3,8 МЛН
HAH Chaos in the Bathroom 🚽✨ Smart Tools for the Throne 😜
00:49
123 GO! Kevin
Рет қаралды 16 МЛН
The joker favorite#joker  #shorts
00:15
Untitled Joker
Рет қаралды 30 МЛН
Laravel DB Optimization: From 1.5k to 26 Queries
14:49
Laravel Daily
Рет қаралды 20 М.
Laravel Junior Code Review: 12 Tips on Everything
15:30
Laravel Daily
Рет қаралды 75 М.
Advanced Laravel: Service Container & Providers (Primitives)
6:06
Jordan Dalton
Рет қаралды 4,6 М.
How to scale Laravel: beyond the basics (Advanced Laravel Scaling)
9:04
Sabatino Talks Dev
Рет қаралды 11 М.
5 tips for supercharged Laravel Eloquent queries
16:29
Andrew Schmelyun
Рет қаралды 68 М.
Why Don't We Have A Laravel For JavaScript?
12:36
Theo - t3․gg
Рет қаралды 104 М.
Junior Code Review: Cleaning Up Laravel CRUD
14:54
Laravel Daily
Рет қаралды 69 М.
Two Things Laravel Services Should NOT Do
8:20
Laravel Daily
Рет қаралды 22 М.
Why is Laravel NOT used in Big Development Projects?
11:53
Stefan Mischook
Рет қаралды 179 М.
OYUNCAK MİKROFON İLE TRAFİK LAMBASINI DEĞİŞTİRDİ 😱
00:17
Melih Taşçı
Рет қаралды 12 МЛН