I think you want something like latestOfMany. (add another relationship for it)
https://laravel.com/docs/10.x/eloquent-relationships#has-one-of-many
I have a one to many relation. I am trying to get all the parent models with only the first child. I have tried :
A::with(['B' => function($q) {
$q->first()
}])->all()
However, this results in the first of A having 1 of B and the rest of A don't have a relation B value.
Create another relationship
//Product Model
public function files()
{
return $this->morphMany(File::class, 'fileable');
}
public function file()
{
return $this->morphOne(File::class, 'fileable');
}
Then use that file relationship if you want to get the first one
Please or to participate in this conversation.