Personally, I would use a Model Event to generate a slug rather than a mutator.
what if i need to add new cast , should i create new function or just append it on
That depends; is the "new cast" depending on the name attribute?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi , i want to use new attribute way stand for laravel 10 , to create a slug from name so i write this code inside Course Model
protected function name(): Attribute
{
return new Attribute(
set: fn($value) => [
'name' => $value,
'slug' => Str::slug($value)]
);
}
It works , but i have some questions is the function name affect on ? and is this way is write way and best practice ? and what if i need to add new cast , should i create new function or just append it on
Please or to participate in this conversation.