F
Filament14mo ago
Askancy

How can I manipulate the response of a post create

Currently my slug system is generated via:
TextInput::make('title')
->required()
->live(onBlur: true)
->afterStateUpdated(fn (string $operation, $state, Forms\Set $set) => $operation === 'create' ? $set('slug', Str::slug($state)) : null),
TextInput::make('slug')
->disabled()
->dehydrated()
->required()
->unique(Article::class, 'slug', ignoreRecord: true),
TextInput::make('title')
->required()
->live(onBlur: true)
->afterStateUpdated(fn (string $operation, $state, Forms\Set $set) => $operation === 'create' ? $set('slug', Str::slug($state)) : null),
TextInput::make('slug')
->disabled()
->dehydrated()
->required()
->unique(Article::class, 'slug', ignoreRecord: true),
so if the item name is: Filamentphp is a package the slug becomes: filamentphp-is-a-package What I would like though, is that in the output it sends to the database, he would add the article id and the extension .html so I would like to become: 1574-filamentphp-is-a-package.html How can I manipulate the response of a post create?
Solution:
you set the property slug to your desired value then save it like normal ``` $article = $this->record; $article->slug = 'slug'; $article->save();...
Jump to solution
5 Replies
Askancy
AskancyOP14mo ago
Ok so I have to use afterCreate, but how do I get the created item id to resave it? with $data['id']? and for save? $data->save()?
awsqed
awsqed14mo ago
try $this->record or $this->getRecord()
Askancy
AskancyOP14mo ago
I was looking at the demo on github because I'm confused anyway about how to act https://github.com/filamentphp/demo/blob/f2a27fd03ba8a8d2c2b1287d6b3949a456e47be7/app/Filament/Resources/Shop/OrderResource/Pages/CreateOrder.php#L21 could you give me some examples?
GitHub
demo/app/Filament/Resources/Shop/OrderResource/Pages/CreateOrder.ph...
Source code for the demo.filamentphp.com website. Contribute to filamentphp/demo development by creating an account on GitHub.
Solution
awsqed
awsqed14mo ago
you set the property slug to your desired value then save it like normal
$article = $this->record;
$article->slug = 'slug';
$article->save();
$article = $this->record;
$article->slug = 'slug';
$article->save();
Want results from more Discord servers?
Add your server