Adding fields to create modal

Hi, i need the permalink to also take the last name, cause right now it takes just the first name, but i don't know how to pass the last name. Any ideas how can i achieve this? public function getCreateForm(): Form { return Form::make([ Input::make() ->name('first_name') ->label('First Name') ->translatable() ->onChange('formatPermalink'), Input::make() ->name('last_name') ->label('Last Name') ->translatable() ->onChange('formatPermalink'), Input::make() ->name('slug') ->label(twillTrans('twill::lang.modal.permalink-field')) ->translatable() ->ref('permalink') ->prefix($this->getPermalinkPrefix($this->getPermalinkBaseUrl())) ]); }
6 Replies
ifox
ifox5mo ago
@oxana do you need your users to be able to update that slug? If not, I recommend not using the slug field And use an accessor to generate the slug from both If you do need users to have control over the slug, you'll need a custom vue component in this case Or rather, a custom formatPermalink function
oxana
oxanaOP5mo ago
yes the user can update the slug. is the function suppose to be something like this Route::get('page-route/{id}', function() {...})->name('page.detail'); Method implementation protected function formData($request) { if ($request->route('page')) { return [ 'customPermalink' => route('page.detail', ['id' => $request->route('page')]), ]; } return []; }
ifox
ifox5mo ago
No, why would it use the id if you want the slug?
oxana
oxanaOP5mo ago
no, I'm saying for the logic is it kinda the same?
ifox
ifox5mo ago
not really. For the slug to generate using multiple fields you need to define an accessor in your model, and use that accessor name in slugAttributes instead of title (or fiest_name in your case).
public function getFullNameAttribute(): string
{
return $this->first_name . ' ' . $this->last_name;
}
public function getFullNameAttribute(): string
{
return $this->first_name . ' ' . $this->last_name;
}
then use first_name in $slugAttributes that way it will generate the slug using the concatenation of both fields. but the thing is this is incompatible with the user editable slug field. that's why, in combination to this, if you want the slug field in the create modal to concatenate automatically on the fly, you'll need some additional JavaScript this is a common use case though, so this might be an opportunity for us to solve it in the core
oxana
oxanaOP5mo ago
i get it now, I'll give it a try. Thank you for explaining this to me, i appreciate it!Glad to have you here!
Want results from more Discord servers?
Add your server