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:Jump to solution
1. on your model, create a
city
relationship to your City model
2. in your table: TextColumn::make('city.name')
...8 Replies
this could be really simple, but did not found any documentation around this as well
Solution
1. on your model, create a
city
relationship to your City model
2. in your table: TextColumn::make('city.name')
thanks @DrByte - I am not using any relationships
is there a way i can do it without it
Why not?
i guess just started without it
hence no
no real reason
use them?
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.
Worked π
it was easy