so can I generate slug based on that unique name field? For example using Observer
Yes you can, just put into your model:
protected static function booted(): void
{
static::creating(function (Recipe $model) {
$model->slug = Str::slug($model->name);
});
}
https://laravel.com/docs/12.x/eloquent#events-using-closures
I see only one issue with such slug creating: names can change over time, what should happen with the slug generated from this name? Should it change automatically? Then old URLs with this slug stop working. Should it remain? Then the slug doesn't correspond to new name, which is wrong too.
My suggestion is to keep generated slugs forever. Put slugs into separate table with MorphToMany relation with all models have slugs. These models have trait HasManySlugs which handles all the logic.
When creating model, you put new slug to slugs table. When updating a model which has slugs, if name changed, you don't update old slug but create new one, maybe marking it "default".
Controllers should handle slugs accordingly, retrieving them from slugs table for corresponding models.
With this approach you never lose your old URLs.