ekazda's avatar
Level 1

Create & Replicate in one action

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 new record.

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.

It feels like the easies way to do this would be to extend the existing replicate functionality into the Create operation when a user makes the "Create and Replicate" selection. Can somebody help me figure out a way to add this functionality to the Create view of a resource?

0 likes
5 replies
experimentor's avatar

@ekazda This is a simple implementation without polluting the database with unwanted replicated records.

Upon creating a new record, send the created record data in the response. Use this data to fill the form values.

I would not recommend creating replicated records in the database for a frontend convenience. If the replicated record is persisted, there will always be one additional record in the database which the user did not want to create.

ekazda's avatar
Level 1

@experimentor I apologize for the lack of clarity. I agree that I don't want to automatically save any additional data within the database. I want to simply hydrate an empty create form with the previously supplied information.

I'm struggle with two aspects of this implementation:

  1. How do I copy the previous data into a new create screen?
  2. How do I add a button at the bottom of the form with the other buttons?

To the first point, I have started to run down the rabbit hole of leaning on the existing replication features and hooking into the redirectAfterCreate function to simply redirect the user to the /resources/resource-name/' . $resource->id . '/replicate URL. This seems to work very well.

I'm now stuck on how to actually identify the user's intentions to "create and replicate". I have this working with an additional field in my fields that is simply a toggle asking the user if they want to run this function on save. This works, but I hate the UI/UX. Doing it this way hijacks both existing save buttons in the capacity that regardless of which button you click ("Create and Add Another" or "Create"), if the user has toggled this value to TRUE then the user is taken to the replicate URL. I don't love this at all.

So this brings me to my #2. I really want to eliminate this toggle and add a button with the other save buttons so that this option is more clear for the user and doesn't hijack the other buttons. How do I add buttons with the other save buttons and have them carry the information I need to fire this replication activity in the redirectAfterCreate function?

Tray2's avatar

It's actually very simple

function store(BookFormRequest $request)
{
	$book = Book::create($request->validate());
    return route('books.create')->with(['book' => $book]);
}

Then in your form you just populate the fields like you would with the edit view.

ekazda's avatar
Level 1

@Tray2 I am not sure I understand your example. Would you mind elaborating a little bit on this? How would this create a new button at the bottom of the create form? How does this receive the $request variable? I've never seen this "store" function and documentation for Nova isn't great, so any help would be appreciated.

experimentor's avatar

@ekazda, I didn't notice that this thread is tagged under Nova. Now it's clear. Your approach using redirectAfterCreate seems to be correct. I have never used Nova. Hence not sure on how to go about adding the additional button. Found one github issue which is almost similar to your case. https://github.com/laravel/nova-issues/issues/2471

Also found one forum post on laracasts on how to add a button in Nova: https://laracasts.com/discuss/channels/nova/can-i-add-a-standalone-button-next-to-the-create-button

Hope these help.

If not, hope some Nova expert notices this thread and provides a solution. Cheers.

Please or to participate in this conversation.