I regularly want to link the related models from the table and created a little macro for it:
Column::macro('linkRecord', function ($view = 'edit') { return $this->url(function ($record) use ($view) { if ($record === null) { return null; } $selectedResource = null; $relationship = Str::before($this->getName(), '.'); $relatedRecord = $record->{$relationship}; if ($relatedRecord === null) { return null; } foreach (Filament::getResources() as $resource) { if ($relatedRecord instanceof ($resource::getModel())) { $selectedResource = $resource; break; } } return $selectedResource::getUrl($view, $relatedRecord->getKey()); });});
For version 3.x you must change almost the last line to:
I find this tidier to implement as a custom column component - then it can be used selectively as required without polluting service providers:
An example in use:
Excellent! Clean code. Works flawlessly with v3
Improved that class a little bit, so you can customize some small things: You can change the viewTybe and use different name to link than the used one :)