Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Amit Merchant:
Laravel Eager Loading - load() Vs. with()
Aug 17, 2017 @ 15:46:11

In a new post to his site Amit Merchant covers the difference between two functions in Laravel's Eloquent functionality that do similar things: load() versus with() for eager loading.

Today, while working with one of my projects(which is built on top Laravel) I bumped into the situation where I needed to get associated model’s data for one of the models. So here, I had two approaches in Laravel to accomplish this which are basically called Eager Loading: with() [and] load().

Both accomplish the same end results - eager loading a related model onto the first. In fact, they both run exactly the same two queries. The key difference is that with() eager loads the related model up front, immediately after the initial query (all(), first(), or find(x), for example); when using load(), you run the initial query first, and then eager load the relation at some later point.

He then goes through examples of each and the queries they produce on the backend. He shows how splitting up the queries might be a better option for some cases and finishes with the benefits of using each (use cases).

tagged: laravel eager loading load with eloquent

Link: https://www.amitmerchant.com/Laravel-Eager-Loading-Load-Vs-With/

Laravel News:
Optimize Eloquent Queries with Eager Loading
Aug 11, 2017 @ 14:23:29

On the Laravel News site they've posted a tutorial sharing some of the basics around the optimizing Eloquent queries with the help of its own "eager loading" feature.

Object Relational mapping (ORM) makes working with databases amazingly simple. While defining database relationships in an object-oriented way makes it easy to query related model data, developers might not pay attention to the underlying database calls.

A standard database optimization for an ORM is eager-loading related data. We will set up some example relationships and then walk through how queries change with and without eager loading. I like to get my hands directly on code and experiment with things, and I hope to illustrate how eager loading works with some examples will further help you understand how to optimize your queries.

They start with a classic example of the "N+1 problem" when working with database records and how, without you knowing, you might be causing it with lazy loading. The article then talks about eager loading vs lazy loading and how they differ in most ORMs. It then covers Eloquent, setting up some migrations for an example blog application and creating the relationships between Author (user) and the Posts. The models are created and seeders are built to populate the tables with Faker data. Finally it gets to the use of eager loading, making use of the "with" functionality to modify the query structure behind the scenes. The post finishes with mentions of two other eager loading types - lazy eager loading and nested eager loading.

tagged: optimize query eager loading laravel eloquent performance nplusone

Link: https://laravel-news.com/eloquent-eager-loading

DevShed:
Lazy and Eager Loading in PHP 5
Sep 11, 2009 @ 17:49:30

On DevShed today there's the start of a new series looking at design patterns in PHP applications. In this first part of the series, they look at lazy and eager loading.

In the case of PHP 5, which is better suited for developing web-based programs, there are two complementary design patterns that can be of great help in building faster and more reliable applications. Of course, as this article’s title suggests, I’m talking about the lazy and eager loading patterns. Despite their rather funny names, they are pretty easy to implement in PHP 5-controlled environments.

To illustrate, they've created a sample class that uses a few class properties and a __toString method to return the values.

tagged: lazy eager loading tutorial designpattern

Link:


Trending Topics: