Display relationship on table column

I currently have a table with 2 of its columns displaying the name of a city and its state respectively. I would like to be able to combine them into one column, but I have tried several approaches and neither one works as I just get no data on the combined column.

I currently have it in two columns like this:

protected function getTableColumns(): array { return [ TextColumn::make('name')->searchable(), TextColumn::make('created_at') ->label('Date') ->date() ->sortable(), TextColumn::make('city.name') ->label('City') ->sortable(), TextColumn::make('city.state.name') ->label('State') ->sortable(), ]; }

The option I tried that to me appeared to be close was using formatStateUsing like this:

TextColumn::make('City, State') ->formatStateUsing(function (Package $package) { if ($package->city && $package->city->state) { return "{$package->city->name}, {$package->city->state->name}"; } return ''; }),

But that still did not work. I think I am doing something wrong or maybe missing something.

That is assuming it is possible to display what I need in one column.

Does anyone have a suggestion/fix that can accomplish displaying the city and state under the same column?
Was this page helpful?