vendredi 10 janvier 2020

Laravel 5.2 eloquent eager loading select columns

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire