R
R
RMate2020-01-19 00:14:48
Laravel
RMate, 2020-01-19 00:14:48

How to sort records by hasOne relationship?

Hey!
There are 2 entities, users and "labels". They are connected by a one-to-one relationship. I need to select all users by sorting by the label name associated with them. Is there a nice way to accomplish this task, without joins, and similar more complex structures?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NubasLol, 2020-01-20
@RMate

return Users::orderByDesc(
    Tag::select('title')
        ->whereColumn('user_id', 'users.id')
        ->orderBy('title', 'desc')
        ->limit(1)
)->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question