Laravel 5.2 eloquent eager loading select columns

Genius in trouble

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:

enter image description here

'relatedProducts' => function($query){
                $query->with('images');
                $query->select(['id','title','description']);
            }

Note: relatedProducts comes from the same table as products. enter image description here

Foued MOUSSI
//...
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.

edited at
0

Comments

0 comments
Login to comment

Related