Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

EckyEckyPtang's avatar

Sorting on "Has one" relation column

Hello,

I have a users and address table. Which is a 1:1 relationship.

So in Nova, I want to make a column 'city' which should be sortable.

Text::make('City', 'address.city')->sortable();

The cities are displayed, but when trying to sort the column by city, it says address.city column is not found. How do I make this sortable?

Cheers

0 likes
4 replies
vincent15000's avatar

Can you explain please, you have for example a table with addresses and you want to sort them according to the city name ?

wingly's avatar

Basically you can't. What you can maybe do is override the index query:

public static function indexQuery(NovaRequest $request, $query)
{
    return $query
        ->join('addresses',  'address_id', '=', 'addresses.id')
        ->select('users.*', 'addresses.city as city');
}

And then

Text::make('City'')->sortable();
EckyEckyPtang's avatar
EckyEckyPtang
OP
Best Answer
Level 3

@wingly That makes sense. But how would that work on the 'edit' page of the resource? Will I be able to change the city there with this 1:1 relation setup?

Would putting everything in the main table be a better solution?

1 like
wingly's avatar

@EckyEckyPtang in the forms you would use a classic relationship field. This you would only display it in the index.

Please or to participate in this conversation.