xxxxxxxxxx
### Controller
if (request()->ajax()) {
$query = Events::orderBy('created_at', 'desc')->get();
return DataTables::of($query)
->addColumn('action', function ($row) {
$btn = '<a href="javascript::void(0);" class="btn btn-primary btn-sm updateEvent" data-id="' . $row->id . '" data-toggle="modal" data-target="#createEvent" data-backdrop="static" data-keyboard="false"><span class="fa fa-edit"></span></a>';
$btn .= ' <a href="' . url('admin/events/delete', $row->id) . '" class="btn btn-danger"><span class="fa fa-trash"></span></a>';
return $btn;
})
->rawColumns(['action'])
->toJson();
}
### Blade File
<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 Server Side Datatables Tutorial</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12 table-responsive">
<table class="table table-bordered user_datatable">
<thead>
<tr>
<th>Ref No</th>
<th>Event Name</th>
<th>State</th>
<th>City</th>
<th>Description</th>
<th>Created Date</th>
<th>Expired Date</th>
<th>Action</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
</body>
<script type="text/javascript">
$(function () {
var table = $('.user_datatable').DataTable({
processing: true,
serverSide: true,
ajax: "{{ url('admin/events') }}",
columns: [
{ data: 'ref_no', name: 'ref_no' },
{ data: 'name', name: 'name', id : 'name' },
{ data: 'state_name', name: 'state_name'},
{ data: 'city_name', name: 'city_name'},
{ data: 'description', name: 'description'},
{ data: 'created_at', name: 'created_at'},
{ data: 'expired_at', name: 'expired_at'},
{ data: 'action', name: 'action'}
]
});
});
</script>
</html>
xxxxxxxxxx
home.blade.php
@extends('layouts.app')
@section('content')
<table id="users-data-table" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Email</th>
<th>Regra</th>
<th>Verificado</th>
<th>Data de criação</th>
{{--<th>Ações</th>--}}
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->role }}</td>
<td>{{ $user->email_verified_at }} </td>
<td>{{ $user->created_at }}</td>
{{--<td>
<a href="{{ route('users.edit', $user->id) }}" class="btn btn-primary">Editar</a>
<form action="{{ route('users.destroy', $user->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Remover</button>
</form>
</td>--}}
</tr>
@endforeach
</tbody>
</table>
app.blade.php
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
</head>
<body>
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#users-data-table').DataTable({
"language": {
"url": "https://cdn.datatables.net/plug-ins/1.11.5/i18n/pt-BR.json"
},
"columnDefs": [
{ "targets": [5], "searchable": false },
{ "bSortable": false, "aTargets": [ 0,5 ] }
],
/* active column position */
"order": [[3, "asc" ]]
});
} );
</script>
</body>
xxxxxxxxxx
$accounts = User::select(
'id',
'username',
'first_name',
'middle_name',
'last_name',
);
$total = $accounts->count();
$result = $accounts->where(
DB::raw(
"CONCAT(
`id`,
`username`,
`first_name`,
`middle_name`,
`last_name`
)"
), 'LIKE', "%".$request->search['value']."%"
);
$result = $accounts->orderBy('first_name', 'ASC')
->skip($request->start)
->take($request->length)
->get();
$filtered = (is_null($request->search['value']) ? $total : $result->count());
// if($request->search['value'] == null) {
// $filtered = $total;
// } else {
// $filtered = $result->count();
// }
return array(
"draw" => $request->draw,
"recordsTotal" => $total,
"recordsFiltered" => $filtered,
"data" => $result,
"request" => $request->all()
);