I am trying to eager load a model in laravel but only return certain columns. I do not want the whole eager loaded table being presented.
public function car
The easiest thing you can do is go by the documentation and do the following thing for getting only specific columns without any extra line of code or closure. Follow steps,
public function car(){
return $this->hasOne('Car', 'id');
}
then when you eager load the relation select only selected columns
$owner = $this->owner
->where('id', 23)
->with('car:id,owner_id,emailid,name') //no space after comma(, ) and id has to be selected otherwise it will give null
Hope this works, have a great day.