I'm trying to get all my users that has a HasMany relation. I want to get the min value of column in relation and retrieve it with the user.
I'm trying this code, it works but it gives me all the model relation, not the one I'm looking for.
first() didn't work as well.
Any suggestion?
$users = User::with(['posts' => function($query) {$query->min('created_at');}])->get();
We can use the
setRelationmethod here to change how we are pulling back to the related post. Using theoldestmethod means we get the oldest post by thecreated_atfield. This can be reversed using thelatestmethod.