Eloquent: Query 3-Level Relations with hasManyThrough and withCount

Поделиться
HTML-код
  • Опубликовано: 26 янв 2025

Комментарии • 46

  • @j.oliveira
    @j.oliveira 4 года назад +23

    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 года назад +3

      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

  • @qudratxojamusayev6785
    @qudratxojamusayev6785 2 года назад

    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

  • @poplach
    @poplach 4 года назад +2

    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.

  • @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 года назад +1

      @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.

  • @belferman
    @belferman 3 года назад +1

    Man, you are really great. Thank you, I learned a lot from you.

  • @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

  • @HassanHafeez007
    @HassanHafeez007 4 года назад +1

    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 года назад +2

      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

    • @HassanHafeez007
      @HassanHafeez007 4 года назад +1

      @@PovilasKorop i agree with your point. And thanks for sharing the video.

  • @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 2 года назад +1

    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  2 года назад

      I think type_id should be a third column in the pivot table child_days

  • @futurefrompast5571
    @futurefrompast5571 2 года назад

    Does it work with paginate nd sorting

  • @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.

  • @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.

  • @bulbul-dev
    @bulbul-dev 4 года назад

    how to save data by relationship?

  • @helderneves91
    @helderneves91 4 года назад +1

    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 года назад +2

      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!

  • @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 года назад +2

      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

  • @belce1982
    @belce1982 3 года назад

    Thanks as always!

  • @raj-kal
    @raj-kal 3 года назад

    Awesome. thanks for the great video :)

  • @kamalsaxena1384
    @kamalsaxena1384 4 года назад +1

    Nice, mysel web developer students next tuotorials plz

  • @webturtlesvlog
    @webturtlesvlog 4 года назад

    Can we make the sub query with specific columns instead of *?
    In your example?

  • @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 года назад +2

      @@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

  • @ahmedfathy3720
    @ahmedfathy3720 4 года назад

    Awesome 👍

  • @sazzadhussain9087
    @sazzadhussain9087 4 года назад

    Good one.

  • @Talkinglife
    @Talkinglife 4 года назад

    Very interesting..

  • @midnightlead
    @midnightlead 4 года назад +3

    В чем приимущество Eloquent в построении сложных запросов? Быстрее и нагляднее написать DB::raw

    • @myzonchuk
      @myzonchuk 4 года назад

      Только не забыть о инъекциях

  • @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