Making slug from 2 input

I'm trying to make a slug from 2 fields but it doesn't works
Forms\Components\TextInput::make('name')
->required()
->maxLength(255)
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
}),

Forms\Components\TextInput::make('last_name')
->required()
->maxLength(255)
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
}),

Forms\Components\TextInput::make('slug')
->required()
->maxLength(255),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255)
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
}),

Forms\Components\TextInput::make('last_name')
->required()
->maxLength(255)
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
}),

Forms\Components\TextInput::make('slug')
->required()
->maxLength(255),
10 Replies
IndomieRendang
IndomieRendang13mo ago
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
Forms\Components\TextInput::make('slug')
->required()
->maxLength(255)
->formatStateUsing(function ($get) {
return str($get('name') . '-' . $get('last_name'))->slug();
}),
Forms\Components\TextInput::make('slug')
->required()
->maxLength(255)
->formatStateUsing(function ($get) {
return str($get('name') . '-' . $get('last_name'))->slug();
}),
Soundmit
SoundmitOP13mo ago
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?
IndomieRendang
IndomieRendang13mo ago
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.
Forms\Components\TextInput::make('name')
->required()
->maxLength(255)
->live(debounce: 500) // maybe ->live(onBlur: true) feels better
Forms\Components\TextInput::make('last_name')
->required()
->maxLength(255)
->live(debounce: 500) // maybe ->live(onBlur: true) feels better
Forms\Components\TextInput::make('slug')
->required()
->maxLength(255)
->formatStateUsing(function ($get) {
// edit the following code as you want to handle more possible cases such as when the name is empty, or when slug already typed manually so we don't want to bother that modifications, etc. I just typed it quickly
return str($get('name') . '-' . $get('last_name'))->slug();
}),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255)
->live(debounce: 500) // maybe ->live(onBlur: true) feels better
Forms\Components\TextInput::make('last_name')
->required()
->maxLength(255)
->live(debounce: 500) // maybe ->live(onBlur: true) feels better
Forms\Components\TextInput::make('slug')
->required()
->maxLength(255)
->formatStateUsing(function ($get) {
// edit the following code as you want to handle more possible cases such as when the name is empty, or when slug already typed manually so we don't want to bother that modifications, etc. I just typed it quickly
return str($get('name') . '-' . $get('last_name'))->slug();
}),
Soundmit
SoundmitOP13mo ago
uhm tried but the slug field remain empty maybe i'm missing something
IndomieRendang
IndomieRendang13mo ago
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_name
josef
josef13mo ago
Although formatStateUsing on the slug field should work
lazydog
lazydog13mo ago
An alternative IF slug can be hidden Update your migration $table->string('slug')->virtualAs('concat(name, \'-\', last_name)');
Soundmit
SoundmitOP13mo ago
yes this can work but i want to keep the slug visible update this is the updated code
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255)
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state . ' ' . $get('last_name')));
}),

Forms\Components\TextInput::make('last_name')
->required()
->maxLength(255)
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($get('name') . ' ' . $state));
}),

Forms\Components\TextInput::make('slug')
->required()
->maxLength(255),
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255)
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state . ' ' . $get('last_name')));
}),

Forms\Components\TextInput::make('last_name')
->required()
->maxLength(255)
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($get('name') . ' ' . $state));
}),

Forms\Components\TextInput::make('slug')
->required()
->maxLength(255),
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)
IndomieRendang
IndomieRendang13mo ago
actually there is a spatie plugin can handle it seamlessly
Soundmit
SoundmitOP13mo ago
it works! Thanks
Want results from more Discord servers?
Add your server