In this tutorial I’m going to describe how to add data in DataTable in laravel 8 we will create single page companies ajax crud application (SPA) using dataTables js, modal and jQuery in laravel 8.
Laravel 8 Ajax CRUD Tutorial using Datatable JS Step 1 – Download Laravel 8 App Step 2 – Database Configuration Step 3 – Installing Yajra Datatables Step 4 – Make Model & Migration Step 5 – Make Routes Step 6 – Create AJAX CRUD Datatables Controller Step 7 – Create Blade Views File companies.blade.php company-action.blade.php Step 8 – Run Development Server
Step 1 — Download Laravel 8 App
1st step Install laravel project and new setup, lets open the terminal C:\xampp\htdocs and paste below code
/* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */
Route::get('/', function () { return view('welcome'); });
use App\Http\Controllers\DataTableAjaxCRUDController;
Lets go to controller and copy below code and paste there
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Company; class DataTableAjaxCRUDController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { if(request()->ajax()) { return datatables()->of(Company::select('*')) ->addColumn('action', 'company-action') ->rawColumns(['action']) ->addIndexColumn() ->make(true); } return view('companies'); }
/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $companyId = $request->id;
/** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // }
/** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $where = array('id' => $request->id); $company = Company::where($where)->first();
return Response()->json($company); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $company = Company::where('id','$request->id')->delete(); return Response()->json($company); } }
Step 6 — Create Blade Views File
Create two blade views file, which is following:
companies.blade.php
Next go to companies.blade.php file and paste below code