laravel get nested relationships data with eager loading

Solutions on MaxInterview for laravel get nested relationships data with eager loading by the best coders in the world

showing results for - "laravel get nested relationships data with eager loading"
Javier
24 May 2017
1/*
2|======================================================================================
3| Get Category With Its Products and Product's First Variant -- Selected Columns Only
4|======================================================================================
5*/
6$category_products = Category::select('id','title','banner_image')
7                             ->where('id',$id)
8                             ->with([
9                                'products' => function($query) {
10                                 	$query->select('id','name','primary_image','category_id');}, 
11                                        'products.firstVariant' => function($query) {
12                                               $query->select('id','retail_price','sale_price','product_id');
13                                        }
14                             ])
15                             ->first();
16