laravel-admin + laravel 5.5 save 2 field with same value

1.6k Views Asked by At

I have a form that needs to generate slug, I use laravel-admin by z-song.

link: https://github.com/z-song/laravel-admin/

In documentation, a form can simply like this:

protected function form()
{
    $form = new Form(new Post);

    $form->text('title');
    $form->hidden('slug');

    return $form;
}

buts it's both manual input. that's not what I need since slug needs to be auto-generated.

I am trying do like this:

protected function form()
{
    $form = new Form(new Post);

    $form->text('title', 'Title');
    $form->hidden('slug')->value(str_slug($form->title));

    return $form;
}

buts its result NULL for the slug one.

so how to make it happen?

1

There are 1 best solutions below

0
RedaMakhchan On

Laravel admin has some callbacks on $form, that can be useful for generating slug case :

use Illuminate\Support\Str;


$form->text('title');
$form->hidden('slug');


$form->saving(function (Form $form) {

    $form->slug = Str::slug($form->title);

});

Note : You can read more about Laravel Helpers, ex. Str::slug.