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.
@PovilasKorop4 жыл бұрын
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.
@midnightlead4 жыл бұрын
João Oliveira it is always the dilemma, but in terms of the project and the task you are right
@poplach4 жыл бұрын
Just wrote the same point of view and then read your comment :D
@belferman3 жыл бұрын
Man, you are really great. Thank you, I learned a lot from you.
@seymourkoop93813 жыл бұрын
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.
@LaravelDaily3 жыл бұрын
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.
@magmaticly3 жыл бұрын
@@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?
@LaravelDaily3 жыл бұрын
@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.
@qudratxojamusayev67852 жыл бұрын
Today I needed exactly this and KZbin offered me this vide at the end of the day. I subscribed to the channel but still the coincidence is something different
@poplach4 жыл бұрын
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.
@HassanHafeez0074 жыл бұрын
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.
@PovilasKorop4 жыл бұрын
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: kzbin.info/www/bejne/hYXHfY2Aeqh8i6c
@HassanHafeez0074 жыл бұрын
@@PovilasKorop i agree with your point. And thanks for sharing the video.
@belce19823 жыл бұрын
Thanks as always!
@raj-kal3 жыл бұрын
Awesome. thanks for the great video :)
@kamalsaxena13844 жыл бұрын
Nice, mysel web developer students next tuotorials plz
@esraaawd13792 жыл бұрын
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.
@webleydevelopment Жыл бұрын
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?
@LaravelDaily Жыл бұрын
I think type_id should be a third column in the pivot table child_days
@LaravelLover3 жыл бұрын
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
@futurefrompast55712 жыл бұрын
Does it work with paginate nd sorting
@ahmedfathy37204 жыл бұрын
I will appreciate that if you explain the self class in a practical example Thank you so much .. You are very helpful
@PovilasKorop4 жыл бұрын
The self class? What do you mean exactly?
@ahmedfathy37204 жыл бұрын
@@PovilasKorop I mean that case when we make a relationship between a model with itself ( self::class ) ... sorry i'm beginner :-)
@PovilasKorop4 жыл бұрын
@@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.
@ahmedfathy37204 жыл бұрын
@@PovilasKorop got it .. Thank you for your time
@sazzadhussain90874 жыл бұрын
Good one.
@helderneves914 жыл бұрын
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!!
@PovilasKorop4 жыл бұрын
Can't answer that question without debugging, I've spent 30 minutes on playing around with the project in this video, before answering.
@helderneves914 жыл бұрын
@@PovilasKorop thanks ;) Will try out!
@ahmedfathy37204 жыл бұрын
Awesome 👍
@Talkinglife4 жыл бұрын
Very interesting..
@evilwizard79312 жыл бұрын
but isn't every customer being loaded as model in a collection then being filtered? I think the other way was optimal.
@bulbul-dev4 жыл бұрын
how to save data by relationship?
@gmarcos20233 жыл бұрын
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.
@amarubhe23814 жыл бұрын
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.
@PovilasKorop4 жыл бұрын
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.
@amarubhe23814 жыл бұрын
Povilas Korop thanks for your opinion
@webturtlesvlog4 жыл бұрын
Can we make the sub query with specific columns instead of *? In your example?
@PovilasKorop4 жыл бұрын
Probably, yes.
@ahmedlist48832 жыл бұрын
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
@midnightlead4 жыл бұрын
В чем приимущество Eloquent в построении сложных запросов? Быстрее и нагляднее написать DB::raw