This laravel tutorial help to add CRUD operation with datatable, I will just share laravel code to create new record, edit record and delete record.
I have already shared How To Use jQuery Datatable With Laravel.We have integrated jQuery datatable with laravel.
We will cover following functionality in this laravel tutorial :
- Create record Template view using bootstrap.
- Create Method to add a new record into Database.
- Update record Template view using bootstrap.
- Update Method to save a edit record values into Database.
- Display a single record data.
- Delete a record from Database.
Laravel CRUD Operation Example Using Datatable
As discussed earlier in this tutorial, I am extending My Previous tutorial.
Create New Record Template
Let’s create a new record functionality into this tutorial.We will add route entry, create a new view file and add logic to handle save record into database.
Step 1: Create a new create.blade.php
file into resources/view/emp
folder.We will add below code into this file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @extends('layouts.master') @section('content') <div class="main-content-inner"> <div class="page-content"> <div class="col-xs-12"> <h3 class="header smaller lighter blue">Add Employee</h3> <form method="post" action="{{ route('emp.store')}}" accept-charset="UTF-8"> <div class="col-md-12"> <div class="form-area"> <div class="alert alert-success hide"></div> <input name="_token" type="hidden" value="{{ csrf_token() }}"/> <div class="alert alert-danger hide"></div> <div class="form-group"> <input type="text" class="form-control" id="employee_name" name="employee_name" value="{{ old('employee_name') }}" placeholder="Name"/> @if ($errors->has('name')) <span class="alert-danger"></span> @endif </div> <div class="form-group"> <input type="number" pattern="[0-9]*" class="form-control" id="employee_salary" name="employee_salary" placeholder="Salary Number" /> </div> <div class="form-group"> <input class="form-control" type="number" id="employee_age" name="employee_age" placeholder="age"></textarea> </div> <button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Add Employee</button> </div> </div> </form> </div> </div> </div> @stop |
Step 2: Create a button to add a new record in index.blade.php
file.
1 2 3 | <div class="clearfix well"> <a href="/emp/create" class="btn btn-primary pull-right">Create</a> </div> |
When we clicked above button, we will redirect to add new record template.
Step 3: Let’s add logic to save record into database. We will add below code into store method in Controllers/EmpController.php
file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public function store(Request $request) { $data = $this->validate($request, [ 'employee_name'=>'required', 'employee_salary'=> 'required|integer', 'employee_age' => 'required|integer' ]); $emp = new Employee([ 'employee_name' => $request->get('employee_name'), 'employee_salary'=> $request->get('employee_salary'), 'employee_age'=> $request->get('employee_age') ]); $emp->save(); return redirect('/emp')->with('success', 'Employee has been added into db.'); } |
Update A Record Into Laravel
We will create method to get data and save updated values into database.
Step 1: Create method to get particular employee data and send to edit template,
1 2 3 4 5 6 | public function edit($id) { $emp = Employee::find($id); return view('emp.update', compact('emp')); } |
Step 2: Create a file update.blade.php
into resources/view/emp
folder.We will add below code into this file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | @extends('layouts.master') @section('content') <div class="main-content-inner"> <div class="page-content"> <div class="col-xs-12"> <h3 class="header smaller lighter blue">Edit Employee - {{$emp->employee_name}}</h3> <div class="col-md-12"> <div class="form-area"> <form role="form" method="post" action="{{ route('emp.update', $emp->id)}}" accept-charset="UTF-8"> <input name="_method" type="hidden" value="PUT"> <div class="alert alert-success hide"></div> <input name="_token" type="hidden" value="{{ csrf_token() }}"/> <div class="alert alert-danger hide"></div> <div class="form-group"> <input type="text" class="form-control" id="name" name="employee_name" value="{{$emp->employee_name}}" placeholder="Name"/> </div> <div class="form-group"> <input type="number" pattern="[0-9]*" class="form-control" id="employee_salary" name="employee_salary" value="{{ $emp->employee_salary }}" placeholder="Salary Number" /> </div> <div class="form-group"> <input class="form-control" type="number" id="employee_age" name="employee_age" placeholder="age" value="{{ $emp->employee_age }}"/> </div> <button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Update Employee</button> </form> </div> </div> </form> </div> </div> </div> @stop |
Step 3: Create a button to edit data from index.blade.php
file.
1 2 3 4 | <td> <!-- edit this nerd (uses the edit method found at GET /nerds/{id}/edit --> <a class="btn btn-small btn-info" href="{{ URL::to('emp/' . $emp->id . '/edit')}}">Edit</a> </td> |
When we clicked above button, we will redirect to edit new record template.
Step 4: We will add below code into update()
method under Controllers/EmpController.php
file.This method is responsible to update data into database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public function update(Request $request, $id) { $data = $this->validate($request, [ 'employee_name'=>'required', 'employee_salary'=> 'required|integer', 'employee_age' => 'required|integer' ]); $emp = Employee::find($id); $emp->employee_name = $request->get('employee_name'); $emp->employee_salary = $request->get('employee_salary'); $emp->employee_age = $request->get('employee_age'); $emp->save(); return redirect('/emp')->with('success', 'Employee has been updated Successfully.'); } |
Show A Record Into Laravel
We will add a button to show details of an employee.We will create method and template to display employee data.
Step 1: Create a method to get particular employee data and send to show template,
1 2 3 4 5 | public function show($id) { $emp = Employee::find($id); return view('emp.show', compact('emp')); } |
Step 2: Create a file show.blade.php
into resources/view/emp
folder.We will add below code into this file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | @extends('layouts.master') @section('content') <div class="main-content-inner"> <div class="page-content"> <div class="col-xs-12"> <h3 class="header smaller lighter blue">Employee Data - {{$emp->employee_name}}</h3> <div class="profile-user-info profile-user-info-striped"> <div class="profile-info-row"> <div class="profile-info-name"> Name </div> <div class="profile-info-value"> {{$emp->employee_name}} </div> </div> <div class="profile-info-row"> <div class="profile-info-name"> Age </div> <div class="profile-info-value"> {{$emp->employee_age}} </div> </div> <div class="profile-info-row"> <div class="profile-info-name"> Salary </div> <div class="profile-info-value"> {{$emp->employee_salary}} </div> </div> </div> <div> </div> </div> </div> </div> @stop |
Step 3: Add a button to display data, we will add below button into index.blade.php
file.
1 2 3 | <td> <a class="btn btn-small btn-success" href="{{ URL::to('emp/' . $emp->id) }}">Show</a> </td> |
Delete A Record Into Laravel
Now, Implements delete record functionality into laravel.
Step 1: Add a button to delete data, we will add below button into index.blade.php
file.
1 2 3 | <td> <a class="btn btn-small btn-success" href="{{ URL::to('emp/' . $emp->id) }}">Show</a> </td> |
Step 2: Delete method to remove particular employee data from database.
1 2 3 4 5 6 7 | public function destroy($id) { $emp = Employee::find($id); $emp->delete(); return redirect('/emp')->with('success', 'Employee has been deleted Successfully.'); } |