Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

Laravel 10. How to order by hasOne relation?

  1. User

    Laravel 10. How to order by hasOne relation?

  2. Assistant

    To order by a hasOne relation in Laravel, you can use the orderBy method and specify the relation's column as the argument. For example, if you have a User model with a hasOne relation to a Profile model, you can order your users by their profile name column like this:

    $users = User::orderBy('profile.name')->get();
    

    If you want to order by a related model's column in descending order, you can use the orderByDesc method instead:

    $users = User::orderByDesc('profile.name')->get();