replicate-in-laravel

Replicate Feature in Laravel

Overview

The Replicate functionality allows you to create a duplicate of a PrefilledSignInSheet record. When the user clicks the "Duplicate" button, the existing record is cloned, and a new entry is created in the database with a modified batch_name and the created_id set to the ID of the currently authenticated user.

Features:

Controller Method: makeDuplicate

This method handles the duplication process of a PrefilledSignInSheet record.

public function makeDuplicate($id)
{
    // Find the original PrefilledSignInSheet record by ID
    $PrefilledSignInSheet = PrefilledSignInSheet::findOrFail($id);

    // Create a new instance by replicating the original record
    $newPrefilledSignInSheet = $PrefilledSignInSheet->replicate();

    // Modify the batch_name to add a timestamp and "Copy" suffix
    $newPrefilledSignInSheet->batch_name = 'Copy of '.$PrefilledSignInSheet->batch_name.' - '.now();

    // Set the creator of the duplicate to the current authenticated user
    $newPrefilledSignInSheet->created_id = Auth::user()->id;

    // Save the new duplicated record
    $newPrefilledSignInSheet->save();

    // Sync the profiles from the original PrefilledSignInSheet to the new one
    $newPrefilledSignInSheet->profiles()->sync($PrefilledSignInSheet->profiles->pluck('id')->toArray());

    // Flash success notification to the session
    $notification = [
        'message' => 'Duplicated Successfully',
        'alert-type' => 'success',
    ];

    // Redirect back to the PrefilledSignInSheet index page
    return redirect()->route('admin.prefilled-sign-in-sheets.index')->with($notification);
}

Breakdown of the Method:

  1. Find the original record:

    • The PrefilledSignInSheet::findOrFail($id) method retrieves the original PrefilledSignInSheet using its ID. If the record is not found, it will throw a ModelNotFoundException.
  2. Replicate the record:

    • The replicate() method creates a duplicate of the original model instance with the same attributes (excluding the primary key and any auto-increment values).
  3. Modify the batch_name:

    • The batch_name is updated by appending a timestamp and - Copy to make it unique. This helps in distinguishing the duplicate from the original.
  4. Set the creator:

    • The created_id field is set to the ID of the currently authenticated user user()->id.
  5. Save the new duplicated record:

    • The save() method persists the duplicated record into the database.
  6. Sync related profiles:

    • The profiles() relationship is synced using the sync() method to attach the same profiles that were associated with the original PrefilledSignInSheet.
  7. Notification:

    • A success notification is flashed to the session, indicating that the duplication was successful.
  8. Redirect:

    • After the duplication process, the user is redirected back to the PrefilledSignInSheet index page with the success notification.

Blade View: Duplicate Button

To allow users to duplicate a PrefilledSignInSheet, add a button in your Blade view file. The button will trigger the makeDuplicate method in the controller.

<a class="btn btn-xs btn-success" href="{{ route('admin.prefilled-sign-in-sheets.duplicate', $prefilled_sign_in_sheet->id) }}"> Duplicate </a>```

- **Button Explanation**:
    - The button is a link (`<a>` tag) that calls the `admin.prefilled-sign-in-sheets.duplicate` route.
    - It passes the `id` of the `PrefilledSignInSheet` to the route, which then invokes the `makeDuplicate` method.

## Route Definition

In your `routes/web.php`, define the route that will handle the duplication:

```php
Route::get('prefilled-sign-in-sheets/duplicate/{id}', 'PrefilledSignInSheetController@makeDuplicate')->name('prefilled-sign-in-sheets.duplicate');

Route Explanation: