F
Filamentβ€’7mo ago
D2RTECH

in Filament v3.x how to show city Name when city ID is saved in the table

I am new to the filament. in the below code I need to show the city name, but because in the table, we have stored just the city ID. Now I can query the city table with the city_id to fetch the city name, but i am not sure how to show that. public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('city_id') ->searchable() ->sortable(), I did not used the relationships to get the city data. here is the code in the forms to get the city ID Forms\Components\Select::make('city_id') ->options(fn (Get $get): Collection => City::query() ->where('state_id', $get('state_id')) ->pluck('name', 'id')) ->searchable() ->preload(),
Solution:
1. on your model, create a city relationship to your City model 2. in your table: TextColumn::make('city.name')...
Jump to solution
8 Replies
D2RTECH
D2RTECHβ€’7mo ago
this could be really simple, but did not found any documentation around this as well
Solution
DrByte
DrByteβ€’7mo ago
1. on your model, create a city relationship to your City model 2. in your table: TextColumn::make('city.name')
D2RTECH
D2RTECHβ€’7mo ago
thanks @DrByte - I am not using any relationships is there a way i can do it without it
DrByte
DrByteβ€’7mo ago
Why not?
D2RTECH
D2RTECHβ€’7mo ago
i guess just started without it hence no no real reason
Jordy
Jordyβ€’7mo ago
use them?
DrByte
DrByteβ€’7mo ago
Filament has already written the code to do what you want by leveraging Laravel Eloquent relationships. So, you can either re-write all the code for custom querying, or just use what Filament offers by doing it the Laravel way.
D2RTECH
D2RTECHβ€’7mo ago
Worked πŸ™‚ it was easy