Answer the question
In order to leave comments, you need to log in
How to make such a selection by links?
Greetings! It would seem a simple thing, but I can not get the result.
There are models:
User - hasMany - Room
Room - hasMany - Order
How to get all relationships of a Room, with one last Order entry?
Tried to do like this:
$user->rooms()
->with([ 'orders' => function ($query) {
$query->with('user')->latest()->first();
}
])
->get();
[
0 => [
'orders' => ['name order']
],
1 => [
'orders' => []
],
2 => [
'orders' => []
],
]
Answer the question
In order to leave comments, you need to log in
It is easier to implement the lastOrder relation in the Room model.
public function lastOrder(): HasOne
{
return $this->hasOne(Order::class, '...')->latest();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question