F
Filament14mo ago
harps

url error on relationship

I have an infolist that renders a piece of text from a relationship and it also links to that entity in the relationship. The problem is that I'm getting an error on the url function when the relationship does not exist. What's the best way to deal with this situation?
TextEntry::make('batch.batch_name')
->label(false)
->url(fn (PipeSurvey $record): string => route('filament.admin.resources.batches.pipes.view', ['record' => PipeBatch::find($record->batch_id)])),
TextEntry::make('batch.batch_name')
->label(false)
->url(fn (PipeSurvey $record): string => route('filament.admin.resources.batches.pipes.view', ['record' => PipeBatch::find($record->batch_id)])),
Solution:
This works. ->url(fn (PipeSurvey $record): string => $record->batch ? PipeBatchResource::getUrl('view', ['record' => $record->batch]) : ''),...
Jump to solution
3 Replies
Shavik
Shavik14mo ago
@harps I used to do links like that but I've had better luck doing them this way. Not sure if this solves your exact problem but maybe it helps?
->url(UserResource::getUrl('view', ['record' => $this->record->creator])),
->url(UserResource::getUrl('view', ['record' => $this->record->creator])),
or like this for you
->url(BatchResource::getUrl('view', ['record' => $this->record-batch])),
->url(BatchResource::getUrl('view', ['record' => $this->record-batch])),
I think this works better too when you have multiple panels as your hard coding to the 'admin' path in the route name would cause issues.
harps
harpsOP14mo ago
That does seem a neater way although it hasn't resolved my issue. I need to avoid running the function when the relationship does not exist. ->url(fn (PipeSurvey $record): string => PipeBatchResource::getUrl('view', ['record' => $record->batch])), Batch doesn't exist on some records it's null 🤔
Solution
harps
harps14mo ago
This works. ->url(fn (PipeSurvey $record): string => $record->batch ? PipeBatchResource::getUrl('view', ['record' => $record->batch]) : ''),
Want results from more Discord servers?
Add your server