Completed
Push — master ( 2b0328...7e8a41 )
by Elf
08:18
created

AdminUserDataTable::getAvatarColumnData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\DataTables;
4
5
use App\Models\AdminUser;
6
use App\Support\Datatables\Services\DataTable;
7
use Illuminate\Support\Facades\Auth;
8
9
class AdminUserDataTable extends DataTable
10
{
11
    /**
12
     * Build DataTable class.
13
     *
14
     * @return \Yajra\Datatables\Engines\BaseEngine
15
     */
16
    public function dataTable()
17
    {
18
        return $this->datatables
19
            ->eloquent($this->query())
20
            ->editColumn('avatar', 'datatables.admin-user-avatar')
21
            ->editColumn('action', function ($user) {
22
                return view('datatables.admin-user-action', compact('user'))->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\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...
23
            })
24
            ->rawColumns(['avatar', 'action']);
25
    }
26
27
    /**
28
     * Get the query object to be processed by dataTables.
29
     *
30
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
31
     */
32
    public function query()
33
    {
34
        $query = AdminUser::query();
35
36
        return $this->applyScopes($query);
37
    }
38
39
    /**
40
     * Optional method if you want to use html builder.
41
     *
42
     * @return \Yajra\Datatables\Html\Builder
43
     */
44
    public function html()
45
    {
46
        return $this->builder()
47
            ->columns($this->getColumns())
48
            ->parameters($this->getBuilderParameters());
49
    }
50
51
    /**
52
     * Get columns.
53
     *
54
     * @return array
55
     */
56
    protected function getColumns()
57
    {
58
        return [
59
            'id' => ['title' => 'ID'],
60
            'avatar' => $this->staticColumn('avatar', ['title' => '头像']),
61
            'username' => ['title' => '用户名'],
62
            'email' => ['title' => '邮箱'],
63
            'created_at' => ['title' => '创建日期'],
64
            'action' => $this->staticColumn('action'),
65
        ];
66
    }
67
68
    /**
69
     * Get the default builder parameters.
70
     *
71
     * @return array
72
     */
73
    protected function getBuilderParameters()
74
    {
75
        return [
76
            'order' => [[0, 'asc']],
77
        ];
78
    }
79
}
80