I am building an API interface for one of my clients eCommerce website and I'm stuck.
Version Laravel 5.2
Problem: Can't select the specific column from the eagerly loaded models.
Route::get('/product/{id}', function($id){
return \App\Models\Product::where('id',$id)->with(
[
'images',
'reviews',
'relatedProducts' => function($query){
$query->with('images');
}
])->first(['id', 'name', 'price', 'description']); // this one is working
});
What i tried:
'relatedProducts:id,title,description' => function($query){
$query->with('images');
}
Error:
'relatedProducts' => function($query){
$query->with('images');
$query->select(['id','title','description']);
}
Note: relatedProducts comes from the same table as products.
//...
return \App\Models\Product::where('id',$id)->with(
[
'images',
'reviews',
'relatedProducts' => function ($q) {
$q->select('id','title','description');
},
'relatedProducts.images'
])->first(['id', 'name', 'price', 'description']);
//...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments