Faster Eloquent: Avoid Accessors with Foreach

Поделиться
HTML-код
  • Опубликовано: 24 сен 2021
  • In this video, step-by-step we will optimize a page loading from 30+ seconds to 0.2 seconds, by avoiding a foreach loop with Eloquent Attributes.
    Spatie Ray: spatie.be/products/ray
    My 20 Laravel Eloquent Tips and Tricks: laravel-news.com/eloquent-tip...
    - - - - -
    Support the channel by checking out our products:
    - Enroll in my Laravel courses: laraveldaily.teachable.com
    - Try our Laravel QuickAdminPanel: bit.ly/quickadminpanel
    - Purchase my Livewire Kit: livewirekit.com
    - View Laravel Code Examples: laravelexamples.com
    - Subscribe to my weekly newsletter: bit.ly/laravel-newsletter
  • ХоббиХобби

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

  • @gillesashley9314
    @gillesashley9314 Год назад +9

    I believe this is by far the best laravel channel on RUclips. Correct me if I'm wrong.

  • @unarmedwombat
    @unarmedwombat 2 года назад +3

    Possibly the single most useful tip I've seen this year. Thanks Povilas.

  • @BsiennKhan
    @BsiennKhan 2 года назад +24

    All of this spesific talk was music for my ears. I love tips on performance tuning, not only performance wise, it also helps you to be a better architect or engineer by following patterns AND improving cognative skills for future projects.

  • @Tooltrocity
    @Tooltrocity 2 года назад +3

    This is a great video, I love learning performance stuff like this. It's the nuts and bolts of a tool that people don't talk about often enough. Please make more videos like this!

  • @mikaelDzp
    @mikaelDzp 2 месяца назад

    thank you! you deserve more views !

  • @obikwelukyrian6848
    @obikwelukyrian6848 2 года назад +2

    Watching your videos has made me an optimization freak tbh. And I'm not complaining 😊

  • @jamzykimani
    @jamzykimani 11 месяцев назад

    Awesome video👏👏 this plus the laravel-news article feels like a level up for me... So much actionable knowledge!!!

  • @LeonelViera
    @LeonelViera 2 года назад +1

    Nice that you elaborate your thinking process

  • @irvingviveros9314
    @irvingviveros9314 2 года назад +1

    New video = instant like!
    Thank you!

  • @rafa.alcantara
    @rafa.alcantara 2 года назад +1

    Povilas GOOD JOB, getting better day after day, Thanks for your incredible amount of work!

  • @jacquesmatike9289
    @jacquesmatike9289 2 года назад +1

    That's a huge optimization ! Thanks !!

  • @edsontomas8344
    @edsontomas8344 Год назад

    Keep dropping them gems. Thank you

  • @abrpp
    @abrpp 2 года назад +1

    Always on point. Thank you!

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

    Su canal es el mejor, he aprendido mucho de usted ¡muchas gracias! buen contenido

  • @mohammadrezaarab7890
    @mohammadrezaarab7890 Год назад

    It was really helpful. thanks

  • @devendergupta_official
    @devendergupta_official 2 года назад +1

    A very good lesson!

  • @sachinvishwakarma290
    @sachinvishwakarma290 2 года назад +1

    Great and very helpful all your laravel video thanks 👍

  • @ChangeYourLifeForever
    @ChangeYourLifeForever 2 года назад +1

    WOOW very intersting , we need more of those videos plz ( explain for us the eager VS normal ) loading data

  • @natenatters
    @natenatters 2 года назад +1

    Nice! Also, like you said at the end about caching, sometimes thats the best solution. You can use the existing logic and set an identity column inside of the User updating model event, then you bypass the need for an accessor.

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

    Great approach! Thx

  • @ahanafi-id
    @ahanafi-id 2 года назад +1

    Great Video, thanks

  • @MrJoboski
    @MrJoboski Год назад

    Amazing!

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

    It is so cool, thank you for this video!:)

  • @intipontt7490
    @intipontt7490 2 года назад +27

    I'm sure there's a perfectly reasonable sql query that returns the same results. This is a perfect example of when NOT to use Eloquent.

    • @Khalyomede
      @Khalyomede 2 года назад +6

      I completely agree with you. I recently found out when using "with('relation')", Eloquent will not perform an inner or left join, it will make a second "select ... where parent_relation in (all ids of parent_relation)", which for me was counter intuitive at first, but in the end to be able to have such a powerful chainable language you need to make some tradeoffs I guess. It still does the job pretty well to have a readable code!

    • @idonnow2
      @idonnow2 2 года назад +1

      My first thought was to make a view in the DB lmao

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

    very good thanks

  • @darkmode404
    @darkmode404 2 года назад +1

    Thank you!

  • @koldovalnya
    @koldovalnya 2 года назад +1

    Thanks 4 tutorial

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

    finally i got what i looking for ( thanks)😊

  • @f00n168
    @f00n168 2 года назад +1

    Thank you 🙏🏻

  • @user-hc6hu9im5f
    @user-hc6hu9im5f 17 дней назад

    awesome ❤

  • @technical157
    @technical157 2 года назад +1

    very interesting. thx

  • @p30mehrdad
    @p30mehrdad 2 года назад +1

    Thanks

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

    How can I add this video to some permanent list ? This is beyond excellent. Thank you so much Povilas!

  • @kkamarada4
    @kkamarada4 2 года назад +1

    Nice work o/

  • @SandorHorvathZ
    @SandorHorvathZ 2 года назад +1

    Thank you so much for this video. Fantastic example how Laravel devs abuse Models nowdays.

  • @TimvandenBelt
    @TimvandenBelt 2 года назад +3

    Once again, a great and educational video! Thanks! However, a small suggestion if you wish to push performance and be more strict with how the app deals with variables (less unexpected behaviour): use identical comparison (===) where possible. I noticed that for several comparisons where it should always be a comparison between two integers, a lossy equation (==) is used.

  • @ricko13
    @ricko13 2 года назад +1

    nuostabus, plojimai!

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

    Good video.
    The most erreur is : use trust orm and don't know how bdd work

  • @Shez-dc3fn
    @Shez-dc3fn 2 года назад +7

    whats baffling me is he does select id from users but in attr he uses $this->name which i assume is an attr of that table.. :/ this name will always be null/empty since its not selected

    • @07Weka
      @07Weka 2 года назад +1

      He discussed and solved that in the video :/

  • @shahsawoodshinwari
    @shahsawoodshinwari 6 месяцев назад

    When I have to choose between a method on eloquent and an accessor, i check if there is any query launched to db, if yes, then it is definetly not an accessor as accessors are loaded on model load, while methods don't.

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

    That's really important topic to follow in development. If you have the right tool then task seems more interesting to do.
    I haven't found the name of this debugging tool in description please share the link.

    • @LaravelDaily
      @LaravelDaily  2 года назад +1

      Composer require barryvdh/laravel-debugbar, also Laravel telescope and Spatie Rsy - those are the tools

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

    Excellent. Could You please link the Ray setup video? If I'm not mistaken You have a nice presentation.

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

    I have an issue, i don't know if its laravel or db issues, but if idle for a certain period of time and tried to retrieve data on the database I got like 1.5k to 3k ms , but on 3rd or 4th call its like on for 400ms, same records it was same records, and I'm using mongodb.

  • @MrFa321
    @MrFa321 2 года назад +1

    Which server are you using in mac for running laravel applications? Like xampp or any other?

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

    Great video! Sometimes there's no reason not to do what the DB is designed to do. On the other hand, there are situation like the following: suppose you have a Posts/Comments project, and you want to show all the comments of a user, but visually divided by post. This is not a "SQL-grouping", cause you don't need to use some aggregate function on the data, but just to have them organized by post. On your opinion, what's a good way to handle these data? I am going with a foreach and an associative array, but doesn't feel right...

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

      Why is it not SQL grouping? When doing the query, you group by post_id, and then in the Blade do foreach ($post) -> foreach ($post->comments) etc.

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

      Because when you group by post_id (in SQL syntax) you aggregate the results using count, sum, etc. That's what I mean with "SQL grouping". Are you referring to using the groupBy function of Laravel?

    • @LaravelDaily
      @LaravelDaily  2 года назад +2

      Yes I thought so, actually thinking about it, it's best to group in collection then, so something like ->get()->keyBy(post_id) or something, don't remember the exact collection method.

    • @mikegodz
      @mikegodz 2 года назад +1

      @@LaravelDaily Didn't know about the keyBy method. I'll dive into it, thanks!

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

    Can you please make a video on recursive function when category needs to be display in hierarchy? Because it causes performance problem

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

      This video should help: ruclips.net/video/5s-_SnVl-1g/видео.html

  • @AbrarAhmad-mz8vl
    @AbrarAhmad-mz8vl 2 года назад

    So basically it is issues with queries itself, not with accessor. Is it? If I'm using just get modify date format will it perform bad too?

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

    Why don't you use laravel ide helper? My perfectionist eyes are bleeding seeing those inspector warnings. But besides aesthetic aspect, ide helper really improves the quality of code by avoiding typos and development speed by autocomplete. I always try to make each php file having green inspector mark, thus, especially if you use type hints, return types and declare strict, green mark says that everything here is correct and you need to pay attention only to the logic

  • @teb1687
    @teb1687 2 года назад +1

    why are comments being deleted?

    • @LaravelDaily
      @LaravelDaily  2 года назад +1

      I haven't deleted any comments, except for obvious spam bots

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

      @@LaravelDaily ok must be some yt issue

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

    Thing is... 700ms is ridiculous for a "production" ready framework

  • @alexaverkiyev9099
    @alexaverkiyev9099 2 года назад +2

    The approach of original code was breaking single responsibility principle and was wrong from the very beginning

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

    I am guilty

  • @david-kahly
    @david-kahly 2 года назад

    not a good example at all. the code and schema should be refactored.

  • @dokumentalny
    @dokumentalny Год назад

    Names of this metod is a straight from hell 😅 is_ghost - rally?!?