I think the first query, even if it looks more complex, is better because you're delegating everything to the database and only getting to PHP the data you need. If you notice, the first query only hydrates 2 models (the ones from the result) where the second hydrates 3 models: it brings all to PHP and then filters them out. If you had 1 million records in the database, if I'm not mistaken, the first query would still hydrate 2 and the second would hydrate 1 million, increasing memory consumption.
Very good point, in this case it's not that many records in the grouped result of the query, in case of more records I would probably consider filtering in MySQL instead of collections.
Today I needed exactly this and RUclips offered me this vide at the end of the day. I subscribed to the channel but still the coincidence is something different
Since first version of filtering (db level) didn't have "Having", probably it is better than doing it on php level, especially if you grab a lot of data from a table where are a lot of users, since in second version of filtering you're filtering array of all possible users (imagine there are 1kk+ or more). The best way is always filtering on db level, but have to be careful with 'Having' statement, since in 'Explain' it doesn't show what it is doing and many times it increases query time 10x times or even more.
I've always thought that plain SQL was simpler and easier than Eloquent, and this video confirms that. What took him 30 minutes to code in Eloquent, I could have done in 30 seconds by simply typing out the correct SQL, and it would have been faster to run. SQL is simpler and faster.
Yes but Eloquent provides not only the "query writing" feature but also stuff like Accessors/Mutators and other things that would not be available if you use plain SQL. So it's not a clear choice, it depends on your situation.
@@LaravelDaily I would watch a video of you, Povilas, demonstrating how to mix models with plain SQL. For example, how could an SQL expert write a pile of SQL and get back a Collection of Model objects? Or, if you have an existing project written with SQL, how could you quickly wrap that in something to get Models with accessors and mutators?
I have s scenario where three tables exists, users, projects and versions, one user belongs to multiple project and versions also. Should I use belongsToManyThrough or what is the better way to do this thing, waiting for your response, thank you
I haven't seen any video of yours in which you used or worked with Laravel filter collection methods. If there is a video or blog of yours on collection filters, then please mention it, or if not then it is my humble request to you that can you please make a video on Laravel Collection filters. And i believe the last refactoring you did with the collection "where" clause could also be done by the filter.
The reason why I don't have videos about collections is that I'm not a big fan of those collection methods. In my opinion, they are not very readable. "Where" statement is much easier to understand than the filter method with a callback function. I do have one video with my opinion: ruclips.net/video/OTdLZJECIUE/видео.html
This is pretty cool. I have a similar but different issue. Do you think you can help? Im working on a platform for nurseries. Children can be assigned to a room each day of the week. I currently have the following (forget the room_child pivot for now) child Model id | name daysOfWeek Model id | day child_days pivot table child_id | day_id dayType Model id | type 1 | full day 2 | morning days_type pivot day_id | type_id So - Want to be able to book each child in for each day of the week. eg child 1 - Monday | Morning Tuesday | Full Day Wednesday | Afternoon Any advice on best way to do this and how to manage it? As in, can I eager load the results or something?
Hello. My English is not very good. I am trying to query from a variable using ORM and have not been able to do it. I could write you an email. Thanks.
Nice! good video. I have a project with this structure: clients hasmany health reports that hasmany weekly follow ups. Imagine I want to get a client name from a follow up. Should I use hasManyThrough? Because weekly follow ups table doesn't have a client_id, but health reports has a client_id. Regards!!
Can you make videos laravel api development with pagination ,filters & multiple sorts . I am not sure why we need to use repository pattern its kind of adding one level of abstraction. And we lose the functionality of route binding in that case.
I'm not a fan of repository pattern either, I don't understand why we need it. I've talked about API in a separate course: laraveldaily.teachable.com/p/how-to-create-laravel-api - but not sure you would find much new information there for yourself, sounds like you're on the right track with your thoughts.
@@ahmedfathy3720 self::class is just an object-oriented syntax in PHP: stackoverflow.com/questions/151969/when-to-use-self-over-this As for relationship to itself, it's a different question: maybe you meant belongsTo, so yes, you can to inside of User model, do function parent() { return $this->belongsTo(User::class, 'parent_id') } or something like that.
First I want to thank you for your tips I think using having more apporatch like this: ->withCount('refunds', function($query) { ... })->having('refunds_count', '>', 0) ->get(); Thanks
I think the first query, even if it looks more complex, is better because you're delegating everything to the database and only getting to PHP the data you need. If you notice, the first query only hydrates 2 models (the ones from the result) where the second hydrates 3 models: it brings all to PHP and then filters them out. If you had 1 million records in the database, if I'm not mistaken, the first query would still hydrate 2 and the second would hydrate 1 million, increasing memory consumption.
Very good point, in this case it's not that many records in the grouped result of the query, in case of more records I would probably consider filtering in MySQL instead of collections.
João Oliveira it is always the dilemma, but in terms of the project and the task you are right
Just wrote the same point of view and then read your comment :D
Today I needed exactly this and RUclips offered me this vide at the end of the day. I subscribed to the channel but still the coincidence is something different
Since first version of filtering (db level) didn't have "Having", probably it is better than doing it on php level, especially if you grab a lot of data from a table where are a lot of users, since in second version of filtering you're filtering array of all possible users (imagine there are 1kk+ or more).
The best way is always filtering on db level, but have to be careful with 'Having' statement, since in 'Explain' it doesn't show what it is doing and many times it increases query time 10x times or even more.
I've always thought that plain SQL was simpler and easier than Eloquent, and this video confirms that. What took him 30 minutes to code in Eloquent, I could have done in 30 seconds by simply typing out the correct SQL, and it would have been faster to run. SQL is simpler and faster.
Yes but Eloquent provides not only the "query writing" feature but also stuff like Accessors/Mutators and other things that would not be available if you use plain SQL. So it's not a clear choice, it depends on your situation.
@@LaravelDaily I would watch a video of you, Povilas, demonstrating how to mix models with plain SQL. For example, how could an SQL expert write a pile of SQL and get back a Collection of Model objects? Or, if you have an existing project written with SQL, how could you quickly wrap that in something to get Models with accessors and mutators?
@Tim Koop sorry, there"s no "quickly" in what you just described :) it's a lot of work, not possible to demonstrate in a short video.
Man, you are really great. Thank you, I learned a lot from you.
I have s scenario where three tables exists, users, projects and versions, one user belongs to multiple project and versions also.
Should I use belongsToManyThrough or what is the better way to do this thing, waiting for your response, thank you
I haven't seen any video of yours in which you used or worked with Laravel filter collection methods. If there is a video or blog of yours on collection filters, then please mention it, or if not then it is my humble request to you that can you please make a video on Laravel Collection filters. And i believe the last refactoring you did with the collection "where" clause could also be done by the filter.
The reason why I don't have videos about collections is that I'm not a big fan of those collection methods. In my opinion, they are not very readable. "Where" statement is much easier to understand than the filter method with a callback function.
I do have one video with my opinion: ruclips.net/video/OTdLZJECIUE/видео.html
@@PovilasKorop i agree with your point. And thanks for sharing the video.
I have a question, it is about update tables with relation in data base level, I displayed the data usig join but I don't know how to update the data.
This is pretty cool. I have a similar but different issue. Do you think you can help?
Im working on a platform for nurseries. Children can be assigned to a room each day of the week. I currently have the following (forget the room_child pivot for now)
child Model
id | name
daysOfWeek Model
id | day
child_days pivot table
child_id | day_id
dayType Model
id | type
1 | full day
2 | morning
days_type pivot
day_id | type_id
So - Want to be able to book each child in for each day of the week. eg
child 1 -
Monday | Morning
Tuesday | Full Day
Wednesday | Afternoon
Any advice on best way to do this and how to manage it? As in, can I eager load the results or something?
I think type_id should be a third column in the pivot table child_days
Does it work with paginate nd sorting
but isn't every customer being loaded as model in a collection then being filtered? I think the other way was optimal.
Hello. My English is not very good. I am trying to query from a variable using ORM and have not been able to do it. I could write you an email. Thanks.
how to save data by relationship?
Nice! good video. I have a project with this structure: clients hasmany health reports that hasmany weekly follow ups. Imagine I want to get a client name from a follow up. Should I use hasManyThrough? Because weekly follow ups table doesn't have a client_id, but health reports has a client_id. Regards!!
Can't answer that question without debugging, I've spent 30 minutes on playing around with the project in this video, before answering.
@@PovilasKorop thanks ;) Will try out!
Can you make videos laravel api development with pagination ,filters & multiple sorts . I am not sure why we need to use repository pattern its kind of adding one level of abstraction. And we lose the functionality of route binding in that case.
I'm not a fan of repository pattern either, I don't understand why we need it. I've talked about API in a separate course: laraveldaily.teachable.com/p/how-to-create-laravel-api - but not sure you would find much new information there for yourself, sounds like you're on the right track with your thoughts.
Povilas Korop thanks for your opinion
Thanks as always!
Awesome. thanks for the great video :)
Nice, mysel web developer students next tuotorials plz
Can we make the sub query with specific columns instead of *?
In your example?
Probably, yes.
I will appreciate that if you explain the self class in a practical example
Thank you so much ..
You are very helpful
The self class? What do you mean exactly?
@@PovilasKorop I mean that case when we make a relationship between a model with itself ( self::class ) ... sorry i'm beginner :-)
@@ahmedfathy3720 self::class is just an object-oriented syntax in PHP: stackoverflow.com/questions/151969/when-to-use-self-over-this
As for relationship to itself, it's a different question: maybe you meant belongsTo, so yes, you can to inside of User model, do function parent() { return $this->belongsTo(User::class, 'parent_id') } or something like that.
@@PovilasKorop got it .. Thank you for your time
Awesome 👍
Good one.
Very interesting..
В чем приимущество Eloquent в построении сложных запросов? Быстрее и нагляднее написать DB::raw
Только не забыть о инъекциях
First I want to thank you for your tips
I think using having more apporatch like this:
->withCount('refunds', function($query) {
...
})->having('refunds_count', '>', 0) ->get();
Thanks