This article introduces how to duplicate (copy) data using the table data ID (primary key) from a screen that displays a list of already registered data, and then display a new registration screen.

Employee Table (employees) Structure

We have prepared an employee table (employees) in which employee names and department names are registered.

Name Description
id Primary key
employee_name Employee name
employee_department Department name

Model for Employee Table (employees) Data

* Prepare the Employee.php file.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{

}

Controller Description for Employee Table (employees) Data List Screen and Duplicate (Copy) New Registration Screen

* Prepare the EmployeesController.php file to display the employee table (employees) data list screen and the duplicate (copy) new registration screen.

Loading the Employee Model

* Load the Employee model and describe the processes such as “employee table (employees) data list screen and duplicate (copy) new registration screen” inside the EmployeeController.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

use App\Employee;   // Use the Employee model
use Validator;  // Use validation
use DB;

class EmployeeController extends Controller
{


/* Describe processes such as the employee table (employees) data list screen and duplicate (copy) new registration screen */


}

Controller Description for the Employee Table (employees) Data List Screen

* Retrieve all data from the employee table (employees).

    public function index(Request $request)
    {
        $employees =  DB::table('employees')->get();

        return view('employees', [
            'employees' => $employees
        ]);
    }

Controller Description for the Duplicate (Copy) New Registration Screen

* Retrieve the data from the employee table (employees) where the ID matches.

    // Duplicate registration screen
    public function copyregister($id)
    {
        $employee = Employee::find($id);

        return view('employeescopyregister', [
            'employee' => $employee,
        ]);
    }

View Description for the Employee Table (employees) Data List and Duplicate (Copy) New Registration Screen

View Description for the Employee Table (employees) Data List Screen

* Prepare the employees.blade.php file to display the employee table (employees) data list.

<table>
    <thead>
        <th>ID</th>
        <th>Employee Name</th>
        <th>Department Name</th>
        <th>&nbsp;</th>
    </thead>
    <tbody>
        @foreach ($employees as $employee)
            <tr>
                <td class="table-text">
                    <div>{{ $employee->id }}</div>
                </td>
                <td class="table-text">
                    <div>{{ $employee->employee_name }}</div>
                </td>
                <td class="table-text">
                    <div>{{ $employee->employee_department }}</div>
                </td>
                <td class="table-text">
                <!-- Copy button -->
                    <form action="{{ url('employeescopyregister/'.$employee->id) }}" method="POST">
                        @csrf
                        <button type="submit">
                            Copy
                        </button>
                    </form>
                </td>
            </tr>
        @endforeach
    </tbody>
</table>

View Description for the Duplicate (Copy) New Registration Screen

* Prepare the employeescopyregister.blade.php file for the duplicate (copy) new registration screen after clicking the “Copy” button on the employee table (employees) data list screen.

<div>
    @include('common.errors')
</div>

<form enctype="multipart/form-data" action="{{ url('employeesentry') }}" method="POST">
    @csrf

   <div>Register by duplicating data with ID “<b>{{$employee->id}}</b>”</div>

   <div>
   <label for="employee_name">Employee Name</label>
   <input type="text" name="employee_name" value="{{ old('employee_name', $employee->employee_name) }}">
   </div>

   <div>
   <label for="employee_department">Department Name</label>
   <input type="text" name="employee_department" value="{{ old('employee_department', $employee->employee_department) }}">
   </div>

    <button type="submit">Register</button>

    <div>
    <a href="{{ url('/employees') }}">
       < Back to List
    </a>
    </div>
</form>

Route Description

* Please change the page URL as needed.
Although it is not described in this article, after submitting the duplicate (copy) new registration screen, send it to “employeesentry” and describe the entry (registration process) in the EmployeeController.

// List display
Route::get('/employees', 'EmployeesController@index');

// Duplicate registration screen
Route::post('/employeescopyregister/{employees}','EmployeesController@copyregister');

// Registration process
Route::post('/employeesentry','EmployeesController@entry');

 
 
* Please use this at your own risk.