CreateAction with form() how to use different model than the current one.

So I have a tasks table that lists different tasks and have actions that you can take on different resources. For example i have a task that it's "add track" and i have a form that creates tracks that i'm using in the manage tracks resource. I extracted it as a shared form and now i'm able to add it to this specific task as such:
Tables\Actions\Action::make('Add Track')
->form(TrackForm::schema())
->icon('iconoir-plus-circle')
Tables\Actions\Action::make('Add Track')
->form(TrackForm::schema())
->icon('iconoir-plus-circle')
When i click on the action button i get the following error [object] (BadMethodCallException(code: 0): Call to undefined method App\Models\Task::trackArtists() this is the track form schema:
class TrackForm
{

public static function schema()
{
return [
Forms\Components\TextInput::make('name')
->columnSpan(2)
->required(),
Forms\Components\Repeater::make('trackArtists')
->relationship()
->hiddenLabel()
->columnSpanFull()
->columns([
'sm' => 4,
])
->schema([
Forms\Components\Select::make('artist_id')
->relationship('artist', 'name')
->getOptionLabelUsing(fn ($value): ?string => Artist::find($value)?->name)
->searchable()
),
];
}
}
class TrackForm
{

public static function schema()
{
return [
Forms\Components\TextInput::make('name')
->columnSpan(2)
->required(),
Forms\Components\Repeater::make('trackArtists')
->relationship()
->hiddenLabel()
->columnSpanFull()
->columns([
'sm' => 4,
])
->schema([
Forms\Components\Select::make('artist_id')
->relationship('artist', 'name')
->getOptionLabelUsing(fn ($value): ?string => Artist::find($value)?->name)
->searchable()
),
];
}
}
I understand the problem and it's that the form its thinking that it needs to use the model from the current resource which is "tasks" I tried something like this:
Tables\Actions\Action::make('Add Track')
->model(Track::class)
->form(TrackForm::schema())
->icon('iconoir-plus-circle')
Tables\Actions\Action::make('Add Track')
->model(Track::class)
->form(TrackForm::schema())
->icon('iconoir-plus-circle')
But not working.. am I missing something? Is this possible to be able to tell the form on this action to use a different model? Thanks
1 Reply
julianwayu
julianwayuOP5mo ago
Ok i managed to get the first part working and getting the form to display in the modal by doing this:
Tables\Actions\CreateAction::make('Add Track')
->mountUsing(function (Form $form) {
$form->model = new Models\Track();
})
->form(TrackForm::schema())
->icon('iconoir-plus-circle')
Tables\Actions\CreateAction::make('Add Track')
->mountUsing(function (Form $form) {
$form->model = new Models\Track();
})
->form(TrackForm::schema())
->icon('iconoir-plus-circle')
but now i get the same error when i create the record.
Want results from more Discord servers?
Add your server