I have a Laravel project which has User and Order models. User model has relation with sorting to Order model like this:
public function last_order() {
return $this->hasOne('Order', 'user_id')->orderBy('created_at', 'desc')->limit(1);
}
This returns last order for specific user. It works fine.
I need to also return sorted array of users from the newest order. I tried to do it with Eloquent on repository layer like this:
User::has('last_order')->with('last_order')->orderBy('last_order.created_at', 'desc')->get();
It doesn't work. I also tried it with sortBy method, same result. It returns me, that last_order table is not exist.
Could you advice me, how to easily sorted some like this with Eloquent (not in service layer, and without raw select)?
Thank you.