Learn How to Use Eager Loading

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.พ. 2025

ความคิดเห็น • 5

  • @BinaryKitten
    @BinaryKitten 2 ปีที่แล้ว +2

    A couple of extra points here.
    for the comments count, it's better to use the ->withCount() method, in this example ->withCount(['comments']) and then using the $ticket->comments_count in the blade view file - this has benefit of doing a count query for you during the load of the ticket itself AND it doesn't need to load/hydrate the collection of comments.
    secondly for the comment author name, this can/should be added as a separate relationship
    public function firstComment(): HasOne
    {
    return $this->hasOne(Comment::class)->oldest('created_at');
    }
    and then you can eager Load the 'first_comment.author' - reducing the size of the data and query down directly to just 1 comment.

    • @esco_tech
      @esco_tech  2 ปีที่แล้ว

      These are great points, thanks for mentioning them!

  • @AmitErandole
    @AmitErandole ปีที่แล้ว

    Very useful. Would you mind sharing a link to this repo or just the factory and seeder files so we can play around?

  • @mohamedsy2968
    @mohamedsy2968 2 ปีที่แล้ว

    thanks for your hard working🤩

    • @esco_tech
      @esco_tech  2 ปีที่แล้ว

      Thank you for the kind words :)