UsersDataTable::dataTable()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 1
dl 0
loc 25
ccs 0
cts 23
cp 0
crap 2
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace App\DataTables;
4
5
use App\User;
6
use Yajra\DataTables\Services\DataTable;
7
8
class UsersDataTable 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(User $user) {
19
                return '<a href="'.route('user.show', $user->id).'">'.$user->name.'</a>';
0 ignored issues
show
Bug introduced by
$user->id of type integer is incompatible with the type array expected by parameter $parameters of route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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