import csv and generate slug before save the post
I need to generate a slug while the records are imported.
I tried to use beforeCreate function but can't find any example about how to use it.
I tried:
but does not seems to work, when import the data I get a sql error because of missing slug.
4 Replies
I would either do it on the fly, so the users can see it. with ->live() in a hidden field.
or tbh, I like using model attributes: setSlugAttribute() and if empty slug, str::slug. But the probably is then you don't have a unique rule. so you need to ideally then create a unique validation too.
I would suggest you use:
mutateFormDataBeforeCreate
When using the import action you can do this in the
resolveRecord()
function (https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import#updating-existing-records-when-importing). You can access $this->data
here, create the slug and then initiate the model with the slug.