Laravel Code Review: Optimize Queries and Other Performance

preview_player
Показать описание
Today I'm reviewing the Artisan command code that I received via email.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

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.

codewithrex
Автор

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.

jcc
Автор

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.

anesu
Автор

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.

jmyers
Автор

this is great sir, thanks for always enlightening us.

timothybelekollie
Автор

I believe you can check if date is during the weekend via Carbon instance method

longingheart
Автор

With the query change, you'll also need to update the data side as shift end_at won't be on a relation.

BinaryKitten
Автор

I only use model query if I actually want to get the model, otherwise I'm just doing a DB::table('***') query

DeepStreamBits
Автор

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.

TrueWebDev
Автор

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"?

ZyronZA
Автор

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

junjunghasudungan
Автор

Is it big "NO" to use DB queries in helper functions or should I cache daily for example to convert currency?

bumblebity
Автор

Very nice but shouldn't weekend be sunday and saturday?

bc
Автор

Hi, I have a question about deleting "second degree relationships." Lets say I have a 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
($grandchild) {
$grandchild->delete();
});

// Delete children
($child) {
// Delete children
$child->delete();
});

// Finally, delete the grandparent
$this->delete();
}

-slash-