i have an audit trails report to show the data based on range of date, selected by user. So user will select the range in page (audits) and when click submit will redirect to page (audits_view).
I want to show the data in datatables, but in audits_view, the result that i get is only raw data like this
{"draw":0,"recordsTotal":490,"recordsFiltered":490,"data":[{"id":144586,"event":"created","created_at":"2021-05-19 10:28:50.660","auditable_type":"App\\Model\\Loglogin","user":null},{"id":144585,"event":"created","created_at":"2021-05-19
here is the audits_view blade and controller
<table id="user_table" class="table table-bordered data-table">
<thead>
<tr>
<th width="35%">auditable_type</th>
<th width="35%">event</th>
</tr>
</thead>
</table>
<script>
$(document).ready(function(){
$('#user_table').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{url('/')}}/report/audits",
type: "POST",
dataType: "json",
'data': function ( d ) {
d._token = "{{ csrf_token() }}";
},
columns: [
{
data: 'auditable_type',
name: 'auditable_type'
},
{
data: 'event',
name: 'event'
},
]
});
});
</script>
here is the controller
public function audits_view(Request $request)
{
$this->authorize('view_audit_trails');
$user = Auth::user();
$date1= $request->input('date1');
$date2= $request->input('date2');
$data = \OwenIt\Auditing\Models\Audit::with('user')
->select(['id', 'event', 'created_at', 'auditable_type'])
->whereDate('created_at','>=',$date1)>whereDate('created_at','<=',$date2)->get();
return DataTables::of($data)
->make(true);
return view('report.audits_view', compact('date1','date2','user'));
My Answer:
"data": [
{
"id": 1,
"user_id": 1,
"action": "created",
"model": "App\\User",
"model_id": 1,
"field": "name",
"old_value": null,
"new_value": "John Doe",
"created_at": "2021-10-01 08:00:00",
"updated_at": "2021-10-01 08:00:00"
},
{
"id": 2,
"user_id": 1,
"action": "updated",
"model": "App\\User",
"model_id": 1,
"field": "email",
"old_value": "john.doe@example.com",
"new_value": "johndoe@example.com",
"created_at": "2021-10-02 09:00:00",
"updated_at": "2021-10-02 09:00:00"
}
]
}
To display this data in a datatable, you can use the following code in your audits_view.blade.php file:
html
ID
User ID
Action
Model
Field
Old Value
New Value
Created At
Updated At
In this code, we are using the DataTables plugin to display the data in a table format. We are making a POST request to the "/audits/data" endpoint to fetch the data based on the selected date range. Make sure to replace the endpoint with the actual endpoint that fetches the audit trail data.
Also, make sure to include the necessary DataTables and jQuery libraries in your audits_view.blade.php file.
Rate this post
3 of 5 based on 5794 votesComments