DevicesDataTable::dataTable()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 1
nop 1
dl 0
loc 20
ccs 0
cts 19
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\DataTables;
4
5
use App\Device;
6
use Yajra\DataTables\Services\DataTable;
7
8
class DevicesDataTable extends DataTable
9
{
10
    /**
11
     * Build DataTable class.
12
     *
13
     * @return \Yajra\DataTables\Engines\BaseEngine
0 ignored issues
show
Bug introduced by
The type Yajra\DataTables\Engines\BaseEngine was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
     */
15
    public function dataTable($query)
16
    {
17
        return datatables($query)
18
            ->editColumn('name', function($device) {
19
                return '<a href="'.route('device.show', $device->id).'">'.$device->name.'</a>';
20
            })
21
            ->addColumn('location', function($device) {
22
                return ($device->location->name ?? 'null');
23
            })
24
            ->addColumn('site', function($device) {
25
                return ($device->location->site->name ?? 'null');
26
            })
27
            ->addColumn('rates', function($device) {
28
                return $device->update_rate.'/'.$device->image_rate.'/'.$device->sensor_rate;
29
            })
30
            ->editColumn('updated_at', function($device) {
31
                return (is_object($device->updated_at) ? $device->updatedAtHuman : 'null');
32
            })
33
            ->blacklist([ 'location', 'site', 'rates' ])
34
            ->rawColumns([ 'name' ]);
35
    }
36
37
    /**
38
     * Get the query object to be processed by dataTables.
39
     *
40
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
41
     */
42
    public function query()
43
    {
44
        $query = Device::query();
45
        return $this->applyScopes($query);
46
    }
47
48
    /**
49
     * Optional method if you want to use html builder.
50
     *
51
     * @return \Yajra\DataTables\Html\Builder
52
     */
53
    public function html()
54
    {
55
        return $this->builder()
56
            ->columns($this->getColumns())
57
            ->minifiedAjax()
58
            ->parameters($this->getBuilderParameters());
59
    }
60
61
    /**
62
     * Get columns.
63
     *
64
     * @return array
65
     */
66
    protected function getColumns()
67
    {
68
        return [
69
            'id',
70
            'name',
71
            'location',
72
            'site',
73
            'open_time',
74
            'close_time',
75
            'rates',
76
            [ 'data' => 'updated_at', 'name' => 'updated_at', 'title' => 'Updated At' ]
77
        ];
78
    }
79
80
    /**
81
     * Get builder parameters.
82
     *
83
     * @return array
84
     */
85
    protected function getBuilderParameters()
86
    {
87
        return [
88
            'dom'     => 'Bfrtip',
89
            'order'   => [ [ 0, 'asc' ] ],
90
            'buttons' => [
91
                [ 'extend' => 'collection', 'text' => '<i class="fa fa-file-excel-o"></i> Export', 'buttons' => [ 
92
                    [ 'extend' => 'csv', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ],
93
                    [ 'extend' => 'excel', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ],
94
                ] ],
95
                [ 'extend' => 'print', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ],
96
                'reset',
97
                'reload',
98
            ],
99
            'paging' => true,
100
            'searching' => true,
101
            'info' => true,
102
            'searchDelay' => 500,
103
        ];
104
    }
105
106
    /**
107
     * Get filename for export.
108
     *
109
     * @return string
110
     */
111
    protected function filename()
112
    {
113
        return 'devices_'.time();
114
    }
115
}
116