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
Sometimes you need to make your relation generic so that can call on your ease.
public function car()
{
return $this->hasOne('Car', 'id');
}
and while fetching you can mention columns you need.
$owners = Owner::with(['car' => function($query) { // eager loading
$query->select('emailid','name');
}
])->get();
foreach($owners as $owner){
$owner->car->emailid;
$owner->car->name;
}