1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\DataTables; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Yajra\DataTables\Services\DataTable; |
8
|
|
|
use Spatie\Activitylog\Models\Activity; |
9
|
|
|
|
10
|
|
|
class ActivityLogDataTable extends DataTable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Build DataTable class. |
14
|
|
|
* |
15
|
|
|
* @return \Yajra\DataTables\DataTableAbstract |
16
|
|
|
*/ |
17
|
|
|
public function dataTable($query) |
18
|
|
|
{ |
19
|
|
|
return datatables($query) |
20
|
|
|
->editColumn('causer_id', function ($activity) { |
21
|
|
|
if ($activity->causer_id) { |
22
|
|
|
return '<a href="' . route($this->getRouteFromType($activity->causer_type), $activity->causer_id) . '">' . $activity->causer->name ?? '' . '</a>'; |
23
|
|
|
} else { |
24
|
|
|
return ''; |
25
|
|
|
} |
26
|
|
|
}) |
27
|
|
|
->editColumn('subject_id', function ($activity) { |
28
|
|
|
if ($activity->subject_id) { |
29
|
|
|
return '<a href="' . route($this->getRouteFromType($activity->subject_type), $activity->subject_id) . '">' . $activity->subject->name ?? '' . '</a>'; |
30
|
|
|
} else { |
31
|
|
|
return ''; |
32
|
|
|
} |
33
|
|
|
}) |
34
|
|
|
->editColumn('properties', function ($activity) { |
35
|
|
|
return $activity->properties; |
36
|
|
|
}) |
37
|
|
|
->editColumn('created_at', function ($activity) { |
38
|
|
|
if ($activity->created_at->diffInDays() > 0) |
39
|
|
|
return $activity->created_at->setTimezone(Auth::user()->timezone)->format('M d, Y h:i a'); |
|
|
|
|
40
|
|
|
else |
41
|
|
|
return $activity->created_at->diffForHumans(); |
42
|
|
|
}) |
43
|
|
|
->rawColumns(['causer_id', 'subject_id', 'properties']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getRouteFromType($type) { |
47
|
|
|
switch ($type) { |
48
|
|
|
case "App\Device": |
|
|
|
|
49
|
|
|
return 'device.show'; |
50
|
|
|
case "App\Deviceimage": |
|
|
|
|
51
|
|
|
return 'image.device'; |
52
|
|
|
case "App\Location": |
|
|
|
|
53
|
|
|
return 'location.show'; |
54
|
|
|
case "App\Sensor": |
|
|
|
|
55
|
|
|
return 'sensor.show'; |
56
|
|
|
case "App\SensorData": |
57
|
|
|
return 'sensordata.show'; |
58
|
|
|
case "App\Site": |
59
|
|
|
return 'site.show'; |
60
|
|
|
case "App\User": |
61
|
|
|
return 'user.show'; |
62
|
|
|
default: |
63
|
|
|
return 'logs'; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get query source of dataTable. |
69
|
|
|
* |
70
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
71
|
|
|
*/ |
72
|
|
|
public function query() |
73
|
|
|
{ |
74
|
|
|
$query = Activity::query(); |
75
|
|
|
return $this->applyScopes($query); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Optional method if you want to use html builder. |
80
|
|
|
* |
81
|
|
|
* @return \Yajra\DataTables\Html\Builder |
82
|
|
|
*/ |
83
|
|
|
public function html() |
84
|
|
|
{ |
85
|
|
|
return $this->builder() |
86
|
|
|
->columns($this->getColumns()) |
87
|
|
|
->minifiedAjax() |
88
|
|
|
->parameters($this->getBuilderParameters()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get columns. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function getColumns() |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'created_at', |
100
|
|
|
[ 'name' => 'causer_id', 'data' => 'causer_id', 'title' => 'Actor', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ], |
101
|
|
|
[ 'name' => 'description', 'data' => 'description', 'title' => 'Action', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ], |
102
|
|
|
[ 'name' => 'subject_id', 'data' => 'subject_id', 'title' => 'Subject', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ], |
103
|
|
|
[ 'name' => 'properties', 'data' => 'properties', 'title' => 'Changes', 'render' => null, 'searchable' => true, 'orderable' => false, 'exportable' => true, 'printable' => true, 'footer' => '' ] |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get builder parameters. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
protected function getBuilderParameters() |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
'dom' => 'Bfrtip', |
116
|
|
|
'order' => [ [ 0, 'desc' ] ], |
117
|
|
|
'buttons' => [ |
118
|
|
|
[ 'extend' => 'collection', 'text' => '<i class="fa fa-file-excel-o"></i> Export', 'buttons' => [ |
119
|
|
|
[ 'extend' => 'csv', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ], |
120
|
|
|
[ 'extend' => 'excel', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ], |
121
|
|
|
] ], |
122
|
|
|
[ 'extend' => 'print', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ], |
123
|
|
|
'reset', |
124
|
|
|
'reload', |
125
|
|
|
], |
126
|
|
|
'paging' => true, |
127
|
|
|
'searching' => true, |
128
|
|
|
'info' => true, |
129
|
|
|
'searchDelay' => 500, |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get filename for export. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
protected function filename() |
139
|
|
|
{ |
140
|
|
|
return 'activitylog_' . time(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: