Completed
Push — master ( b8fc4b...08375c )
by Mohamed
10:19
created

UserDataTable::dataTable()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 9.296
c 0
b 0
f 0
cc 4
nc 1
nop 1
1
<?php
2
3
namespace Microboard\DataTables;
4
5
use App\User;
6
use Yajra\DataTables\DataTableAbstract;
7
use Yajra\DataTables\Html\Builder;
8
use Yajra\DataTables\Html\Button;
9
use Yajra\DataTables\Html\Column;
10
use Yajra\DataTables\Services\DataTable;
11
12
class UserDataTable extends DataTable
13
{
14
    /**
15
     * Build DataTable class.
16
     *
17
     * @param mixed $query Results from query() method.
18
     * @return DataTableAbstract
19
     */
20
    public function dataTable($query)
21
    {
22
        return datatables()
0 ignored issues
show
Bug introduced by
The method eloquent does only exist in Yajra\DataTables\DataTables, but not in Yajra\DataTables\DataTableAbstract.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
23
            ->eloquent($query)
24
            ->editColumn('avatar', '<img src="{{ $avatar }}" class="avatar-sm" alt="{{ $name }}" />')
25
            ->addColumn('action', function (User $user) {
26
                $html = '';
27
28
                if (auth()->user()->can('view', $user)) {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
29
                    $html .= '<a href="' . route('microboard.users.show', $user) . '" ' .
30
                        'class="table-action" data-toggle="tooltip" ' .
31
                        'data-original-title="' . trans('microboard::users.view.action-button') . '">' .
32
                        '<i class="fas fa-eye"></i></a>';
33
                }
34
35
                if (auth()->user()->can('update', $user)) {
36
                    $html .= '<a href="' . route('microboard.users.edit', $user) . '" ' .
37
                        'class="table-action" data-toggle="tooltip" ' .
38
                        'data-original-title="' . trans('microboard::users.edit.action-button') . '">' .
39
                        '<i class="fas fa-edit"></i></a>';
40
                }
41
42
                if (auth()->user()->can('delete', $user)) {
43
                    $html .= '<form action="' . route('microboard.users.destroy', $user) . '" method="post" class="d-inline-block">' .
44
                        csrf_field() . method_field('DELETE') .
45
                        '<button type="submit" data-toggle="tooltip" ' .
46
                        'class="bg-transparent border-0 p-0 table-action table-action-delete" ' .
47
                        'data-original-title="'. trans('microboard::users.delete.action-button') .'" ' .
48
                        'data-modal-title="'. trans('microboard::users.delete.title') .'" ' .
49
                        'data-modal-text="'. trans('microboard::users.delete.text') .'" ' .
50
                        'data-confirm="'. trans('microboard::users.delete.confirm') .'" ' .
51
                        'data-cancel="'. trans('microboard::users.delete.cancel') .'">' .
52
                        '<i class="fas fa-trash"></i></button></form>';
53
                }
54
55
                return $html;
56
            })
57
            ->escapeColumns(['*']);
58
    }
59
60
    /**
61
     * Get query source of dataTable.
62
     *
63
     * @param User $model
64
     * @return \Illuminate\Database\Eloquent\Builder
65
     */
66
    public function query(User $model)
67
    {
68
        return $model->newQuery();
69
    }
70
71
    /**
72
     * Optional method if you want to use html builder.
73
     *
74
     * @return Builder
75
     */
76
    public function html()
77
    {
78
        return $this->builder()
79
            ->language(trans('microboard::datatable', []))
80
            ->addTableClass('table table-striped table-hover')
81
            ->columns($this->getColumns())
82
            ->setTableId('user-table')
83
            ->autoWidth(false)
84
            ->orderBy(0)
85
            ->minifiedAjax()
86
            ->dom("<'card'" .
87
                "<'card-header border-0'<'row align-items-center'<'col-12 col-sm-8 col-md-6'<'row no-gutters'<'col-4'l><'col-8'f>>><'col-12 col-sm-4 col-md-6 text-right'B>>>" .
88
                "<'table-responsive't>" .
89
                "<'card-footer'<'row'<'col-12 col-sm-6'i><'col-12 col-sm-6'p>>>" .
90
                "r>")
91
            ->buttons(
92
                Button::make('print')->text('<span>' . trans('microboard::datatable.print') . '</span><i class="fa fa-print"></i>'),
93
                Button::make('excel')->text('<span>' . trans('microboard::datatable.excel') . '</span><i class="fa fa-file-excel"></i>'),
94
                Button::make('reload')->text('<span>' . trans('microboard::datatable.reload') . '</span><i class="fa fa-sync"></i>')
95
            );
96
    }
97
98
    /**
99
     * Get columns.
100
     *
101
     * @return array
102
     */
103
    protected function getColumns()
104
    {
105
        return [
106
            Column::make('id')->title(trans('microboard::users.fields.id'))->width('1%'),
107
            Column::make('avatar')->title(trans('microboard::users.fields.avatar'))->width(36),
108
            Column::make('name')->title(trans('microboard::users.fields.name')),
109
            Column::make('email')->title(trans('microboard::users.fields.email')),
110
            Column::computed('action', '')
111
                ->exportable(false)
112
                ->printable(false)
113
                ->width('1%')
114
                ->addClass('text-right')
115
        ];
116
    }
117
118
    /**
119
     * Get filename for export.
120
     *
121
     * @return string
122
     */
123
    protected function filename()
124
    {
125
        return 'User_' . date('YmdHis');
126
    }
127
}
128