Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel Live Vietnam 2022: Laravel Eloquent Performance

Laravel Live Vietnam 2022: Laravel Eloquent Performance

Dinh Quoc Han

March 25, 2023

More Decks by Dinh Quoc Han

Other Decks in Technology


  1. Laravel Vietnam Who? • My name is Dinh Quoc Han

    / Đinh Quốc Hân • I'm a PHP Lover, Laravel fan! • 7 years experience in software development • Currently working at SHIFT ASIA • Admin at Laravel Vietnam
  2. Laravel Vietnam Laravel Eloquent is Model Eloquent is an object

    relational mapper (ORM) that is included by default within the Laravel framework.
  3. Laravel Vietnam Requirement Show 3 blocks: 1. Total posts 2.

    Total published posts 3. Total draft posts
  4. Laravel Vietnam Example We have a comments list shown in

    the post. Comment::class has an accessor `url`, it will be the link of the comment (The post URL with comment fragment). So, it will be a duplicate query statement if shown on the post detail page. Let me show you.
  5. Laravel Vietnam Duplicated SQL Statements • The post detail •

    The post in the comment eager loader 👉 It took 2 statements but is the same.
  6. Laravel Vietnam When? If you have a lot of scope

    methods in the model and you won't find a way to extract this into the new class. This is exactly the way you want.
  7. Laravel Vietnam When? If you have a lot of logic

    collection methods with the model. E.g: The positive price invoice lines. You don't need to rewrite logic in many places.