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:
- Duplicates the existing record's fields.
- Appends a timestamp to the
batch_namefield to differentiate it from the original. - Associates the same profiles to the duplicated sheet.
- Tracks who created the duplicate (using
created_id). - Redirects the user back to the
PrefilledSignInSheetindex page with a success notification.
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:
-
Find the original record:
- The
PrefilledSignInSheet::findOrFail($id)method retrieves the originalPrefilledSignInSheetusing its ID. If the record is not found, it will throw aModelNotFoundException.
- The
-
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).
- The
-
Modify the batch_name:
- The
batch_nameis updated by appending a timestamp and- Copyto make it unique. This helps in distinguishing the duplicate from the original.
- The
-
Set the creator:
- The
created_idfield is set to the ID of the currently authenticated user user()->id.
- The
-
Save the new duplicated record:
- The
save()method persists the duplicated record into the database.
- The
-
Sync related profiles:
- The
profiles()relationship is synced using thesync()method to attach the same profiles that were associated with the originalPrefilledSignInSheet.
- The
-
Notification:
- A success notification is flashed to the session, indicating that the duplication was successful.
-
Redirect:
- After the duplication process, the user is redirected back to the
PrefilledSignInSheetindex page with the success notification.
- After the duplication process, the user is redirected back to the
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:
- The route maps the URL
prefilled-sign-in-sheets/duplicate/{id}to themakeDuplicatemethod in thePrefilledSignInSheetController. - The
{id}parameter is the ID of thePrefilledSignInSheetyou want to duplicate.