skip to Main Content

I am trying to create a custom button at the bottom of a resource that will save the record and then take all of the information in that record and duplicate it into another record.

Laravel Nova Buttons

This duplicated/copied information would then give the user a jumpstart to enter another record. Right now the only actions on the Create and Update pages are Create/Update, Create/Update and Add Another but those two buttons don’t fit all circumstances. And the last option just provides a blank form. I want the previously entered information to be auto-added to the form to save time.

And I am aware of the replicate function. But this requires the record to be saved and the user to return to the record and then select the replicate option. When I have users entering hundreds of items at a time, this extra step is very time consuming.

Can somebody help me figure out a way to add additional buttons to the forms so that I can extend the create and update functionality?

2

Answers


  1. Have you tried using the Replicate action on a resource? I believe this does exactly what you are looking for! You can read more about replicate at https://nova.laravel.com/docs/resources/

    Click the three dots on the resource to view the actions

    Login or Signup to reply.
  2. If you need a quick solution to pre-hydrate the form fields for a new resource with the same values as the last resource created, you could try something like:

    public function fields(NovaRequest $request) {
    
        $latestResource = null;
        if ($request->isCreateOrAttachRequest()) {
            $latestResource = Account::latest('id')->first();
        }
    
        return [
            ID::make()->sortable(),
    
            Text::make(__('First Name'), 'first_name')
                ->withMeta([
                    'value' => $latestResource ? $latestResource->first_name : $this->first_name
                ]),
    
            Text::make(__('Last Name'), 'last_name')
                ->withMeta([
                    'value' => $latestResource ? $latestResource->last_name ??  $this->last_name
                ]),
         ];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search