laravel get nested relationships data with eager loading
/*
|======================================================================================
| Get Category With Its Products and Product's First Variant -- Selected Columns Only
|======================================================================================
*/
$category_products = Category::select('id','title','banner_image')
->where('id',$id)
->with([
'products' => function($query) {
$query->select('id','name','primary_image','category_id');},
'products.firstVariant' => function($query) {
$query->select('id','retail_price','sale_price','product_id');
}
])
->first();