Making slug from 2 input
I'm trying to make a slug from 2 fields but it doesn't works
10 Replies
Do you want to combine them?
If yes, just make the name and last_name live, then make the slug field get and combine from those two fields...
Maybe as simple as this
yes, is what i mean but i can't understand your solution, i'm quite new with filament
when the get on the slug is fired?
The livewire is updated when there are changes on live components, and all properties will be re-evaluated on every livewire update, which means $get values are also updated.
uhm tried but the slug field remain empty maybe i'm missing something
Oops... sorry, I think it is not formatStateUsing...
Let's back to your original code above... If we want to combine both fields, then we need to get both values...
so
$set('slug', Str::slug($state . ' ' . $get('last_name'));
for name
and $set('slug', Str::slug($get('name') . ' ' . $state);
for last_nameAlthough formatStateUsing on the slug field should work
An alternative IF
slug
can be hidden
Update your migration
$table->string('slug')->virtualAs('concat(name, \'-\', last_name)');
yes this can work but i want to keep the slug visible
update
this is the updated code
but it doen't works
when i type the name 'jhon' the slug is set to john
then i type the last_name 'smith' nothing change in the slug
then i modify the name 'jhon sam' and now the slug looks like john-sam-smith
the same if i start from the last_name
it seems that if you start from name, only the name can change the slug
i don't know the virtualAs, very interesting
also storedAs.
which is better for a slug?
what happen if i have 2 John Smith in the database?
what happen to the second slug? (slug need to be unique)
actually there is a spatie plugin can handle it seamlessly
it works! Thanks