Custom CreateAction in getHeaderActions redirects to Createpage of Resource

I try to make a CreateAction on a Resource to create a record of a different model. This worked in Filamentphp 3. I updated to Filamentphp 4 and now this is not working any more. To not trap into some self-made problems like cache i created a new app from scratch with FilamentPHP 4. Now i stripped everything down and have a Foo and a Bar Model with Resources and i addes a CreateAction to ViewBar.php:
<?php

namespace App\Filament\Resources\Bars\Pages;

use App\Filament\Resources\Bars\BarResource;
use Filament\Actions\EditAction;
use Filament\Resources\Pages\ViewRecord;

use Filament\Actions\CreateAction;
use Filament\Forms\Components\TextInput;

class ViewBar extends ViewRecord
{
protected static string $resource = BarResource::class;

protected function getHeaderActions(): array
{
return [
EditAction::make(),
CreateAction::make()
->schema([
TextInput::make('title')
->required()
->maxLength(255),
// ...
])
];
}
}
<?php

namespace App\Filament\Resources\Bars\Pages;

use App\Filament\Resources\Bars\BarResource;
use Filament\Actions\EditAction;
use Filament\Resources\Pages\ViewRecord;

use Filament\Actions\CreateAction;
use Filament\Forms\Components\TextInput;

class ViewBar extends ViewRecord
{
protected static string $resource = BarResource::class;

protected function getHeaderActions(): array
{
return [
EditAction::make(),
CreateAction::make()
->schema([
TextInput::make('title')
->required()
->maxLength(255),
// ...
])
];
}
}
So this is the exact example vom https://filamentphp.com/docs/4.x/actions/create#introduction It should show a form with the field "title" when i click on it but the "new bar" button links to http://127.0.0.1:8001/admin/bars/create?record=1 and shows me the form to create a new Bar record The code from my original app is this:
Actions\CreateAction::make()
->label("Buchen")
->model(Booking::class)
->form(Booking::getForm())
];
Actions\CreateAction::make()
->label("Buchen")
->model(Booking::class)
->form(Booking::getForm())
];
But it does the same -> redirect to the Create-Form of the Resource i put it in... Am i doing something wrong? Thank you.
Solution:
ok i think we are getting closer. maybe i'm a little disapointed because it was super intuitive in v3 and now just different 😉 this works: ```php...
Jump to solution
6 Replies
toeknee
toeknee4w ago
I think calling the createAction natively does that. Just instead do an Action
Actions\Action::make()
->label("Buchen")
->model(Booking::class)
->form(Booking::getForm())
->action(function($data) => {
Booking::create($data);
Notification::make('success')->success()->title('Booking Created');
})
];
Actions\Action::make()
->label("Buchen")
->model(Booking::class)
->form(Booking::getForm())
->action(function($data) => {
Booking::create($data);
Notification::make('success')->success()->title('Booking Created');
})
];
DigitalInfinity
DigitalInfinityOP4w ago
Thank you for your suggestion. Good idea but it only woks if i use a form without relations. If i use this:
Action::make("book")
->label("Book")
->model(Booking::class)
->schema([
TextInput::make('title')->required()->maxLength(255),
Select::make('invoice_id')->label('Invoice')->relationship(name: 'invoice', titleAttribute: 'name')
])
Action::make("book")
->label("Book")
->model(Booking::class)
->schema([
TextInput::make('title')->required()->maxLength(255),
Select::make('invoice_id')->label('Invoice')->relationship(name: 'invoice', titleAttribute: 'name')
])
I get an error "The relationship [invoice] does not exist on the model [App\Models\Todo]." So it still hangs on Todo model and somehow the ->model() method is not working
toeknee
toeknee4w ago
Off the top of my head Can you try ->record(null) on the action and ->fillForm([]) Does seem bug worthy report that, since an action that defines a model should use that model class and not the current resource model... Wonder if action picks up Record or form fill hence my suggesiton above
DigitalInfinity
DigitalInfinityOP3w ago
it looks like this but that does not work:
Action::make("book")
->record(null)
->fillForm([])
->model(Booking::class)
->label("Buchen")
->schema(Booking::getform())
->action(function($data) {
Booking::create($data);
Notification::make('success')->success()->title('Booking Created');
})
Action::make("book")
->record(null)
->fillForm([])
->model(Booking::class)
->label("Buchen")
->schema(Booking::getform())
->action(function($data) {
Booking::create($data);
Notification::make('success')->success()->title('Booking Created');
})
Still just redirects me to the create form of the resource where i use this I'll upload an app to reproduce this and test on a page outside a resource tomorrow and then... open an issue?
awcodes
awcodes3w ago
You shouldn’t have to define a record, fillform or model for this. It’s just an action with a form and you handle the creation in the action call back. Could be a weird situation too where the order of the method chain matters.
Solution
DigitalInfinity
ok i think we are getting closer. maybe i'm a little disapointed because it was super intuitive in v3 and now just different 😉 this works:
Action::make('createBooking')
->label('Book')
->schema([
TextInput::make('title')
->label("Titel")
->required(),
Select::make('invoice_id')
->label('Invoice')
->relationship(name: 'invoice', titleAttribute: 'name')
])
->record(fn () => new Booking)
->action(function (array $data): void {
Booking::create($data);
}),
Action::make('createBooking')
->label('Book')
->schema([
TextInput::make('title')
->label("Titel")
->required(),
Select::make('invoice_id')
->label('Invoice')
->relationship(name: 'invoice', titleAttribute: 'name')
])
->record(fn () => new Booking)
->action(function (array $data): void {
Booking::create($data);
}),
booking has a relationship to invoice. but: when i remove the record() it tries to resolve this for the resource, i'm in (ToDo) which results in an error that there is no such relationship i can live with this. thanks everybody for helping. if i can figure out to use CreateAction for this again, i'll come back here 🙂

Did you find this page helpful?