php - Eloquent ORM dynamic relation properties vs method for eager loading -
i'm using eloquent in slim framework. take advantage of eager loading. wonder there difference between using property , method.
the reason i'm asking because i'm using twig template engine , can call method.
will able take advantage of eager loading using property or not?
$obj = new book(); $books = $books->with('author')->get(); // using property foreach ($books $book) { echo $book->author->name; } // using method foreach ($books $book) { echo $book->author()->get()->name; }
there huge difference. in second line populated $books
authors. , when iterating through them in first foreach have data, don't perform additional sql queries. eager loading made for.
but in second foreach calling get()
method on every iteration , laravel starts repopulating authors making queries on , on every book.
Comments
Post a Comment