Completed
Pull Request — master (#82)
by Brandon
02:15
created

SensorDataDataTable::html()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\DataTables;
4
5
use Yajra\DataTables\Services\DataTable;
6
use App\SensorData;
7
8
class SensorDataDataTable extends DataTable
9
{
10
    /**
11
     * Build DataTable class.
12
     *
13
     * @return \Yajra\DataTables\Engines\BaseEngine
14
     */
15
    public function dataTable($query)
16
    {
17
        return datatables($query)
18
            ->editColumn('sensor_id', function ($sensordata) {
19
                return '<a href="/sensor/' . $sensordata->sensor_id . '">'. ($sensordata->sensor->name ?? '') . '</a>';
20
            })
21
            ->addColumn('action', 'sensordata.action')
22
            ->blacklist([ 'action'])
23
            ->rawColumns(['sensor_id', 'action']);
24
    }
25
26
    /**
27
     * Get the query object to be processed by dataTables.
28
     *
29
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
30
     */
31
    public function query()
32
    {
33
        $query = SensorData::query();
34
        return $this->applyScopes($query);
35
    }
36
37
    /**
38
     * Optional method if you want to use html builder.
39
     *
40
     * @return \Yajra\DataTables\Html\Builder
41
     */
42
    public function html()
43
    {
44
        return $this->builder()
45
            ->columns($this->getColumns())
46
            ->minifiedAjax()
47
            ->parameters($this->getBuilderParameters());
48
    }
49
50
    /**
51
     * Get columns.
52
     *
53
     * @return array
54
     */
55
    protected function getColumns()
56
    {
57
        return [
58
            'id',
59
            'sensor_id',
60
            'value',
61
            'created_at',
62
            'updated_at',
63
            'action'
64
        ];
65
    }
66
67
    /**
68
     * Get builder parameters.
69
     *
70
     * @return array
71
     */
72 View Code Duplication
    protected function getBuilderParameters()
73
    {
74
        return [
75
            'dom'     => 'Bfrtip',
76
            'order'   => [ [ 0, 'desc' ] ],
77
            'buttons' => [
78
                'create',
79
                'export',
80
                'print',
81
                'reset',
82
                'reload',
83
            ],
84
            'paging' => true,
85
            'searching' => true,
86
            'info' => true,
87
            'searchDelay' => 500,
88
        ];
89
    }
90
91
    /**
92
     * Get filename for export.
93
     *
94
     * @return string
95
     */
96
    protected function filename()
97
    {
98
        return 'sensordata_'.time();
99
    }
100
}
101