F
Filament8mo ago
anjama

Handling null id in column url to related resource

I'm using urls in my tables to allow quick navigation to the views of related records. Here's an example:
TextColumn::make('bird_id')->sortable()->searchable()
->url(fn (Capture $record): string => BirdResource::getUrl('view', ['record' => $record->bird_id]))
TextColumn::make('bird_id')->sortable()->searchable()
->url(fn (Capture $record): string => BirdResource::getUrl('view', ['record' => $record->bird_id]))
It generally works great, but in one of my tables the column of related ids has null values. These null values cause errors, so they have to be handled. What would be a good approach to conditionally showing these urls when the values are not null?
Solution:
->url(fn (?int $state): string => $state ? static::getUrl('view', ['record' => $state]) : null)
->url(fn (?int $state): string => $state ? static::getUrl('view', ['record' => $state]) : null)
?...
Jump to solution
2 Replies
Solution
LeandroFerreira
LeandroFerreira8mo ago
->url(fn (?int $state): string => $state ? static::getUrl('view', ['record' => $state]) : null)
->url(fn (?int $state): string => $state ? static::getUrl('view', ['record' => $state]) : null)
?
anjama
anjamaOP8mo ago
Thanks. Had to mark the return type as nullable, but otherwise it works and in a way simplifies what I was doing before
->url(fn (?int $state): ?string => $state ? BirdResource::getUrl('view', ['record' => $state]) : null),
->url(fn (?int $state): ?string => $state ? BirdResource::getUrl('view', ['record' => $state]) : null),
Want results from more Discord servers?
Add your server