3

I have a relationship inside my laravel model

/**
 * Relation with calculations table
 *
 * @return object
 */
public function calculations()
{
    return $this->hasMany('App\Calculation');
}

When i am selecting data with relation as

$this->diamonds
->select('id', 'image', 'number', 'weight', 'diamond_date', 'price')
->with('calculations')->first();

It returns all data and works fine, but when i want to select specific column it returns [] blank array

$this->diamonds
->select('id', 'image', 'number', 'weight', 'diamond_date', 'price')
->with(['calculations', function($query){
     $query->select('id', 'height', 'width')
}])->first();

I search lot and every one suggest to select data with this type but i don't know why data is empty while i am selecting specific column.

Karan
  • 1,146
  • 1
  • 10
  • 24
Rajat Sharma
  • 369
  • 2
  • 10

1 Answers1

3

Remember that the primary key (id in this case) is necessary in the $query->select() to actually retrieve the necessary results.*

$this->diamonds->select('id', 'image', 'number', 'weight', 'diamond_date', 'price')
     ->with(['calculations', function($query){
             $query->select('id', 'diamond_id', 'height', 'width')
      }])->first();
Karan
  • 1,146
  • 1
  • 10
  • 24