Eloquent Performance: TOP 3 Mistakes Developers Make

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

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

  • @PauloRicardo-dk6yf
    @PauloRicardo-dk6yf 3 года назад +3

    I am Brazilian and like very much your videos.

  • @mahdihassannejad6157
    @mahdihassannejad6157 3 года назад +3

    I should thank you a lot Povilas, I always start my day by watching one of your videos, And it would opens my mind along the day.

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

    i like how you categorize your video with the related topic
    for example in this video you prefix title with "Eloquent Performance:" it will be so good when someone search for performance in channel 👏👏

  • @sodomousprime
    @sodomousprime 3 года назад +12

    The last one is like when you have a HTML select input, where you only need the "id" and the "title" rather than loading all of the rest of the data. Good video, cheers.

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

      Model::pluck('title', 'id');
      Be careful, do not do
      Model::get()->pluck('title', 'id');
      As it does a Select * and then call pluck on the collection

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

      Exactlly i see a lot of people doing that.

  • @Bevallalom
    @Bevallalom 3 года назад +15

    In certain cases it's better to use raw sql then query builder. Also proper db indexing is important factor.

  • @Stoney_Eagle
    @Stoney_Eagle 3 года назад +10

    When a TV show has casts, crews, posters, backdrops, translations, trailers and seasons, seasons has translations and episodes, episodes has translations and a video file and video file has a progress you learn quick to do all these eager loadings on the database level, not to mention using the right indexes on the keys your relations uses.
    That saves 2 to 5 minutes and 500mb on the worst case scenario 😅

  • @maxpower7735
    @maxpower7735 3 года назад +5

    When using eager loading you may need to check if the data was loaded. For that Laravel has the function "relationLoaded".

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

    Very specific and detail example. Very helpful.

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

    Another way to explain $user->post->count() is that when we load a model either with relationship or not the returned data is a collection instance. And in the collection documentation we have ->count() method that counts data from the returned collection and not from databse.

  • @KirillHybrid-f4f
    @KirillHybrid-f4f 3 года назад +5

    One more important thing, it's very easy to use model $with variable to eager load relationship. But in much cases this data isn't used for example in model index.
    In that cases you can see how the performance decrease, specially with polymorphic relationship. It's better to retrieve relationships only for show and edit methods, where they will be really used, but not always through the model $with.

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

    Thanks a lot again Povilas for making these videos.

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

    Great! I made the 3rd mistake too much when I made PDF reports that contained more than 10,000 records. Instead of creating a ReportResource with only the fields needed for the report, I used a Resource that returned all the fields in the table. For many records, it makes a total difference in the total size of json that the frontend needs to download from the backend.

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

    Do you mind sharing those examples where PHP manipulation was seen more speedy than MySQL? Please share your experiences.
    Eager to learn more from you. Respect from Pakistan.

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

      I don't remember them, to be honest, it was a long time ago, I just remember the surprised feeling about it.

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

    Thank you sir for sharing it. I would like to know which front end framework do you suggest for a mid beginner of Laravel ? Live wire; vue, reactjs, jet-stream ,…..

  • @tim.bogdanov
    @tim.bogdanov 2 года назад

    Whoa, this is huge, I've been using with incorecctly. Thank you

  • @user-ej4dm2xq6q
    @user-ej4dm2xq6q 3 года назад +1

    Very helpful. Thank you.

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

    I do filtering in PHP if whether the user can see an object is based on some permissions. For example, different users in a store have different payment methods available. I can't think of a simple way to filter that in the database. In PHP, you can use Collection::filter() to check if $user->can().

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

    Thanks as always. Saved to watch later. Keep the hard work!

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

    Such great content! Thank you very much!!!!

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

    Thanks alot for these advices!

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

    Nice video Povilos 👍
    Keep up the good work.

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

    I new on laravel thanks for your helpfull video. How do u can view that's laravel process windows down there ?. I had no idea how to do that.

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

      Composer require barryvdh/laravel-debugbar

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

      @@LaravelDaily ooh it was composer library, I thought it was some new laravel feature. thanks

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

    What is the better: $user->posts->count() or count($user->posts) ?

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

    Hi sir, thanks for tips. Can you tell us, is there any tool for REST API debugging for laravel like Laravel Debugbar?

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

      I think you can use Laravel Telescope. Don't quote me on that though as I am still relatively new to the whole Laravel experience.

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

      Telescope and Ray, a few videos about them: ruclips.net/user/LaravelDailysearch?query=debug%20api

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

      You could use itsgoingd/clockwork. It has a similar feature to debugbar to add a footer with some request information, and in my opinion is a bit simpler than Telescope.

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

      I think you can still use the Debugbar but you have to use the backend url with /api/posts like localhost: 8000/api/posts

  • @RehmanKhan-zv2rl
    @RehmanKhan-zv2rl 3 года назад

    Great, I was always confuse to use relation with '( )' or without '( )', and also with eager loading, i have cleared the difference between that but can you please define more about eager loading and lazy loading and stuff like that, It would be really helpful.
    Thanks

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

    am using join and leftjoin function in some cases instead of laravel relationship i find that way is better for me do you have any suggestions ?

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

    Informative, thanks for sharing.

  •  3 года назад

    Hi Povilas, nice video. Which is your current stack for php/laravel development locally?
    I am using docker locally, but your video it seems very faster.

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

      Laravel valet on my MacBook

    •  3 года назад

      @@LaravelDaily Tks for the answer. It looks very good. Docker became lazy because of the sync of files between container/local env even using :delegated

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

    For the last case, I have a similar problem.
    In my database, I have almost 13.000 records of users. I have to process them all to get stats of users by checking an attribute value (0, 1, 2, 3). Loading data from database is fast, but process them in the foreach takes 40s. And obviously only that attribute and ID are loaded.
    Is there a way to do that operation for minimum time?

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

      Send me the code to povilas@laraveldaily.com maybe it's worth shooting a video about.

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

    @LaravelDaily I liked your videos much. Rather I say, I am learning Laravel by watching your videos only.
    I have a doubt.
    At 1:30, 2:25 or 5:05 you are showing us DB queries count and all DB queries. How can I see it in my browser. Is there any specific software or chrome extension for it?
    Also, please share list of all extensions and softwares that you use while development.

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

    Wow, really good tips

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

    This was helpful, thank you

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

    Thank you very much sir

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

    How would you do an API to debug this? Please, it would be very interesting to attack this type of problem, basically because my stack in more than 200 projects is always Vue.js and Laravel API

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

    When you query only a select few columns of a relationship, should we do that within the with() call, or in the get() call?

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

      It should be within with()
      For example retrieving comment relation with required fields;
      with('comment:id,user_id,text,created_at')

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

      @@gabrielilochi7840 Thanks mate 👍

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

    Thanks, quite useful

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

    Good,but how to get the user avatar in or other map fields in this query.

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

    Thank you.

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

    thanks for these tips

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

    Thanks sir

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

    could you review Encore\Admin\Layout\Content approach for rendering views ( from z-song/laravel-admin package)

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

      I don't think this individual case would be very useful for the audience.

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

    any alternative to debugbar for laravel + SPA configs?

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

    Thanks

  • @amitsolanki9363
    @amitsolanki9363 9 месяцев назад

    In laravel debugger What does the model indicate? Like models 300, like models 120

    • @LaravelDaily
      @LaravelDaily  9 месяцев назад +1

      The amount of Laravel eloquent models saved to RAM during this request.

    • @amitsolanki9363
      @amitsolanki9363 9 месяцев назад

      How can I reduce amount of Laravel eloquent models

  • @paul.stream24
    @paul.stream24 3 года назад

    May i know what is the package name or extension you use like inspect elements for laravel?

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

    you look like Mauricio Pochettino

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

    Using Withcount takes 5.15 ms whereas with eager loading it takes only 2.1 ms which one is better in terms of performance

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

    Hi sir Can u make a video on upload large CSV in db fast using queue I worked on it its working fine but due to my server limitation sometimes queue fails

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

      Fastest: use mysql LOAD DATA INFILE
      Or laravel lazy collections + chunked inserts

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

      @@ClCfe i am already using laravel jobs by breaking large csv into small files chunks then using jobs to save in background using particular user id

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

    What should I do if I want to join across 4 four tables? The "hasManyThrough" seems only support 3 tables.

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

    Thanks.

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

    Thx

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

    what is the benefit of compact() ?

  • @RANJEETKUMAR-wz4dg
    @RANJEETKUMAR-wz4dg 3 года назад

    thanks

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

    hi sir i still not understand about ur relasi using withCount(posts) and u get data in index using $user->post_count , and ur relasi name is posts , do u can tell me about $user->post_count coz ur name relasi is posts not post_count . thanks

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

      Laravel docs: laravel.com/docs/8.x/eloquent-relationships#counting-related-models

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

    What console log are you using for this?

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

      Laravel Debugbar

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

      @@LaravelDaily can I use it when I only use laravel as an api endpoint?

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

    Excuse me, sir I have seen two methods in the Eloquent part are `fresh` and `refresh` BUT I do NOT know what is the benefit of these methods So, can you please explain more about these methods?

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

      Where exactly have you seen them, in what context? Can you give me the example/link?

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

      @@LaravelDaily I can NOT send the link because every time I send a link it deleted So, search for `Eloquent: Getting Started` in the docs then you'll find the `Refreshing Models` title

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

    How to use laravel debugger ? Can you please assist me in this regard?

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

      Google "laravel Debugbar" and read its documentation

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

      @@LaravelDaily Thanks a lot for your kind reply. I have installed successfully.

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

    How do we know if we create an inefficient query?

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

      Hard to answer in a RUclips comment. Emmm, you measure it against other possible queries?

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

    What happens when I don't solve N+1 problems? Someone helps. Thanks

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

      App performance will be drastically reduced...

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

    Great as alwayes

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

    Who can I test these kind of mistakes for API request or controller

  • @AbdelrahmanEl.Monshed
    @AbdelrahmanEl.Monshed 2 года назад

    good

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

    How to secure .evn file in laravel.

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

      Don't push it to GitHub

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

      @@LaravelDaily Thank you sir for reply. but actually my issue it is. I have make a Laravel 5.8 project In which many API are make for android APP. And one user find a bug his using our app and get some error. It shows the code of the .evn file. so how to secure .evn file to get any error.

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

    amaizng

  • @DenisK-v7z
    @DenisK-v7z 3 года назад

    Spasibo!

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

    Awesome

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

    Good video thanks , can you please make a video on how to design a database digram I struggle a lot with it
    Please like this comment for him to see this

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

      I have a full course: laraveldaily.teachable.com/p/how-to-structure-database-in-laravel
      Also, an old video: ruclips.net/video/RbKEYDtkAJI/видео.html

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

    Sir,
    I need to load 10 different kind from database news table based on their category which would be faste
    method 1:-
    $news_cat_1 = News::where('cat_id','1')->get();
    $news_cat_2 = News::where('cat_id','2')->get();
    .
    .
    .
    $news_cat_10 = News::where('cat_id','10')->get();
    Method 2:
    $allNews = News::all();
    $news_cat_1 = $allNews->where('cat_id','1')->get();
    $news_cat_2 = $allNews->where('cat_id','2')->get();
    .
    .
    .
    .
    $news_cat_10 = $allNews->where('cat_id','10')->get();