Today, We want to share with you Server side Laravel DataTables example.In this post we will show you DataTables Server-side Processing with Laravel 5.8, hear for Laravel 5.8 – DataTables Server-side Processing we will give you demo and example for implement.In this post, we will learn about Laravel 5.8 Ajax Crud Tutorial using DataTables with an example.
Server side Laravel DataTables example
There are the Following The simple About Server side Laravel DataTables example Full Information With Example and source code.
As I will cover this Post with live Working example to develop How to implement DataTables server-side in laravel, so the datatables jquery plugin with php mysql and bootstrap for this example is following below.
Create Laravel 5.8 View
resources/views/product.blade.php
Laravel 5.8 - DataTables Server-side Processing
Id Title Body Created At Options
Create Laravel Model, controller and route
Create Laravel Route
Route::post('allproducts', 'productController@allproducts' )->name('allproducts');
The Product model code is given below.
htdocs/app_name/app/Models/Product.php
Now the code for allproduct function in productController.
Create Laravel Controller
productController.php
htdocs/app_name/app/Http/Controllers/productController.php
public function allproducts(Request $request) { $columns = array( 0 =>'id', 1 =>'title', 2=> 'body', 3=> 'created_at', 4=> 'id', ); $totalData = Product::count(); $totalFiltered = $totalData; $limit = $request->input('length'); $start = $request->input('start'); $order = $columns[$request->input('order.0.column')]; $dir = $request->input('order.0.dir'); if(empty($request->input('search.value'))) { $products = Product::offset($start) ->limit($limit) ->orderBy($order,$dir) ->get(); } else { $search = $request->input('search.value'); $products = Product::where('id','LIKE',"%{$search}%") ->orWhere('title', 'LIKE',"%{$search}%") ->offset($start) ->limit($limit) ->orderBy($order,$dir) ->get(); $totalFiltered = Product::where('id','LIKE',"%{$search}%") ->orWhere('title', 'LIKE',"%{$search}%") ->count(); } $data = array(); if(!empty($products)) { foreach ($products as $product) { $show = route('products.show',$product->id); $edit = route('products.edit',$product->id); $all_products_data['id'] = $product->id; $all_products_data['title'] = $product->title; $all_products_data['body'] = substr(strip_tags($product->body),0,50)."..."; $all_products_data['created_at'] = date('j M Y h:i a',strtotime($product->created_at)); $all_products_data['options'] = " "; $data[] = $all_products_data; } } $json_data = array( "draw" => intval($request->input('draw')), "recordsTotal" => intval($totalData), "recordsFiltered" => intval($totalFiltered), "data" => $data ); echo json_encode($json_data); }Web Programming Tutorials Example with Demo
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about Server side Laravel DataTables example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.