Why we use with() method in laravel and what is difference between laravel eloquent Eager loading and Lazy loading.
Laravel eloquent lazy loading
We have a model and it takes some relationships with other models. If we get Model data after accessing its relationship then run a SQL query to get that relationship record then it means model data pass in a foreach loop and each iteration we get relationship record and it runs SQL query it called lazy loading, We have total N records than it runs total N+1 SQL queries. We also understand many queries do slow execution.
Let take an example of lazy loading.
TablesTables posts -> id, name, image extra_details -> id, post_id, descriptionPost Model
We have a Post model and it take a 'postDetail' hasOne relationship with PostDetail model.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function postDetail() { return $this->hasOne(PostDetail::class); } }PostController
Here we get all posts in index method.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; class PostController extends Controller{ public function index(){ $Posts = Post::all(); foreach($posts as $post){ echo "Post Name".$post->name; echo "Post description".$post->postDetail->description; } } }
In the above example $posts variable take only posts record and when we get description data which is a column of extra_details table and it runs extra query every time for getting 'postDetail' relationship records it is lazy loading.
Laravel eloquent eager loading
For preventing lazy loading slow execution laravel provide eager loading which runs only one query and gets all model and its relationship record with. Laravel provides a with() method for loading relationship records with main query. We use one or more relationships in with method.
Let an example of with() method
PostControllerHere we get all posts with its relationship 'extraDetail' in index method.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; class PostController extends Controller{ public function index(){ $Posts = Post::with('postDetail')->get(); foreach($posts as $post){ echo "Post Name".$post->name; echo "Post description".$post->postDetail->description; } } }
In above example 'postDetail' relationship records already loaded with Post model show in loop now extra query required.
Difference between eager and lazy loading
The main difference between eager and lazy loading is eager loading get all data with relationship records in single query and lazy loading require N+1 queries for getting main model and relation data.
Eager loading run single query whereas lazy loading run N+1 queries.
Eager loading execution speed fast and take less memory compare to lazy loading.
Here we explain laravel eloquent concepts with advanced web tutorial. Also provide laravel tutorial with hindi videos.
Anmol Sharma
I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.
Related tutorial links
- part 1 laravel hasOne() relationship
- part 2 laravel one to many relationship
- part 3 Has One Of Many relationship
- part 4 laravel hasOneThrough relationship
- part 5 laravel hasManyThrow() relationship
- part 6 laravel many to many relationship
- part 7 one to one polymorphic relationship
- part 8 laravel one to many polymorphic relationship
- part 9 many-to-many polymorphic relationship
- part 10 laravel eloquent has vs whereHas method
- part 11 laravel whereDoesntHave and whereDoesntHave
- part 12 laravel withCount() method
- part 13 eager vs lazy loading