Eloquent: Query 3-Level Relations with hasManyThrough and withCount

  Рет қаралды 32,755

Laravel Daily

Laravel Daily

Күн бұрын

Пікірлер: 46
@j.oliveira
@j.oliveira 4 жыл бұрын
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.
@PovilasKorop
@PovilasKorop 4 жыл бұрын
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.
@midnightlead
@midnightlead 4 жыл бұрын
João Oliveira it is always the dilemma, but in terms of the project and the task you are right
@poplach
@poplach 4 жыл бұрын
Just wrote the same point of view and then read your comment :D
@belferman
@belferman 3 жыл бұрын
Man, you are really great. Thank you, I learned a lot from you.
@seymourkoop9381
@seymourkoop9381 3 жыл бұрын
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.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
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.
@magmaticly
@magmaticly 3 жыл бұрын
@@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?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
@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.
@qudratxojamusayev6785
@qudratxojamusayev6785 2 жыл бұрын
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
@poplach
@poplach 4 жыл бұрын
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.
@HassanHafeez007
@HassanHafeez007 4 жыл бұрын
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.
@PovilasKorop
@PovilasKorop 4 жыл бұрын
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
@HassanHafeez007
@HassanHafeez007 4 жыл бұрын
@@PovilasKorop i agree with your point. And thanks for sharing the video.
@belce1982
@belce1982 3 жыл бұрын
Thanks as always!
@raj-kal
@raj-kal 3 жыл бұрын
Awesome. thanks for the great video :)
@kamalsaxena1384
@kamalsaxena1384 4 жыл бұрын
Nice, mysel web developer students next tuotorials plz
@esraaawd1379
@esraaawd1379 2 жыл бұрын
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
@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
@LaravelDaily Жыл бұрын
I think type_id should be a third column in the pivot table child_days
@LaravelLover
@LaravelLover 3 жыл бұрын
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
@futurefrompast5571
@futurefrompast5571 2 жыл бұрын
Does it work with paginate nd sorting
@ahmedfathy3720
@ahmedfathy3720 4 жыл бұрын
I will appreciate that if you explain the self class in a practical example Thank you so much .. You are very helpful
@PovilasKorop
@PovilasKorop 4 жыл бұрын
The self class? What do you mean exactly?
@ahmedfathy3720
@ahmedfathy3720 4 жыл бұрын
@@PovilasKorop I mean that case when we make a relationship between a model with itself ( self::class ) ... sorry i'm beginner :-)
@PovilasKorop
@PovilasKorop 4 жыл бұрын
@@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.
@ahmedfathy3720
@ahmedfathy3720 4 жыл бұрын
@@PovilasKorop got it .. Thank you for your time
@sazzadhussain9087
@sazzadhussain9087 4 жыл бұрын
Good one.
@helderneves91
@helderneves91 4 жыл бұрын
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!!
@PovilasKorop
@PovilasKorop 4 жыл бұрын
Can't answer that question without debugging, I've spent 30 minutes on playing around with the project in this video, before answering.
@helderneves91
@helderneves91 4 жыл бұрын
@@PovilasKorop thanks ;) Will try out!
@ahmedfathy3720
@ahmedfathy3720 4 жыл бұрын
Awesome 👍
@Talkinglife
@Talkinglife 4 жыл бұрын
Very interesting..
@evilwizard7931
@evilwizard7931 2 жыл бұрын
but isn't every customer being loaded as model in a collection then being filtered? I think the other way was optimal.
@bulbul-dev
@bulbul-dev 4 жыл бұрын
how to save data by relationship?
@gmarcos2023
@gmarcos2023 3 жыл бұрын
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.
@amarubhe2381
@amarubhe2381 4 жыл бұрын
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.
@PovilasKorop
@PovilasKorop 4 жыл бұрын
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.
@amarubhe2381
@amarubhe2381 4 жыл бұрын
Povilas Korop thanks for your opinion
@webturtlesvlog
@webturtlesvlog 4 жыл бұрын
Can we make the sub query with specific columns instead of *? In your example?
@PovilasKorop
@PovilasKorop 4 жыл бұрын
Probably, yes.
@ahmedlist4883
@ahmedlist4883 2 жыл бұрын
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
@midnightlead
@midnightlead 4 жыл бұрын
В чем приимущество Eloquent в построении сложных запросов? Быстрее и нагляднее написать DB::raw
@myzonchuk
@myzonchuk 4 жыл бұрын
Только не забыть о инъекциях
Laravel Eloquent: Deeper Relationships with One Query
10:37
Laravel Daily
Рет қаралды 144 М.
Eloquent or Query Builder: When to Use Which?
5:48
Laravel Daily
Рет қаралды 89 М.
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 30 МЛН
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 9 МЛН
Optimizing Eloquent: Running Out Of Memory?
6:11
Laravel Daily
Рет қаралды 43 М.
Cache Eloquent Query Results to Load Pages Instantly
5:43
Laravel Daily
Рет қаралды 61 М.
Junior Code Review: Laravel Routes, Middleware, Validation and more
19:57
4 Tips for Dev Productivity (I've tried many techniques...)
9:01
Laravel Daily
Рет қаралды 2,2 М.
Eloquent Performance: TOP 3 Mistakes Developers Make
7:59
Laravel Daily
Рет қаралды 49 М.
Laravel: BelongsTo vs Polymorphic? Let's Test!
14:09
Laravel Daily
Рет қаралды 26 М.
Laravel Pivot Tables: Simple to Advanced Many-to-Many
12:24
Laravel Daily
Рет қаралды 121 М.
Laravel Migrations: 12 Useful Tips in 12 Minutes
12:06
Laravel Daily
Рет қаралды 81 М.
Laravel Security: Top 7 Mistakes Developers Make
11:16
Laravel Daily
Рет қаралды 87 М.