How to set $record for Custom Action

I have an action that i want to easily use throughout my app and I am having issues figuring out how to set the record that is being used. Its working fine as. a table action as the record is passed automatically and even fine as a form action is a Property record, but lets say I have another form that is a User record that has a property relationship. I want to be able to use this action there, but unfortuantely i get a type mismatch issue as its trying to use the User model and not the Property model. Unfortunately its not as simple as setting ->record() on the action. This is what my current action looks like (simplified a bit):
<?php

namespace App\Actions\Filament\Forms;

use App\Models\Property;
use Filament\Forms\Components\Actions\Action;
use Illuminate\Contracts\View\View;

class ViewPropertyAction extends Action
{
protected function setUp(): void
{
$this
->label('View')
->modalContent(fn (Property $record): View => view(
'forms.property',
['record' => $record],
))
->extraModalFooterActions([
\Filament\Tables\Actions\Action::make('edit')
->label('Edit Property')
->url(function ($record) {
//return route('properties.edit', $this->modalRecord ? $this->modalRecord->id : $record->id);
return route('properties.edit', $record->id);
})
->color('primary')
->button(),
]);
}
}
<?php

namespace App\Actions\Filament\Forms;

use App\Models\Property;
use Filament\Forms\Components\Actions\Action;
use Illuminate\Contracts\View\View;

class ViewPropertyAction extends Action
{
protected function setUp(): void
{
$this
->label('View')
->modalContent(fn (Property $record): View => view(
'forms.property',
['record' => $record],
))
->extraModalFooterActions([
\Filament\Tables\Actions\Action::make('edit')
->label('Edit Property')
->url(function ($record) {
//return route('properties.edit', $this->modalRecord ? $this->modalRecord->id : $record->id);
return route('properties.edit', $record->id);
})
->color('primary')
->button(),
]);
}
}
and I just call it with
ViewPropertyAction::make('view_property')
ViewPropertyAction::make('view_property')
. Wish i could just do
ViewPropertyAction::make('view_property')->record($model)
ViewPropertyAction::make('view_property')->record($model)
or whatever. Any suggestions?
1 Reply
Mark Chaney
Mark ChaneyOP16mo ago
Not ideal, but does work
->modalContent(function ($record): View {
if (! $record instanceof Property) {
$record = Property::find($record->property_id);
}

return view(
'forms.property',
['record' => $record],
);
})
->modalContent(function ($record): View {
if (! $record instanceof Property) {
$record = Property::find($record->property_id);
}

return view(
'forms.property',
['record' => $record],
);
})
Not the most flexible solution, but might have to be it. Would still prefer to be able to pass it when i call the action Though i kind of need a way to get a property from the parent page too. Hmm eh, this isnt reliable. need to find another way
Want results from more Discord servers?
Add your server