AttributeTable::filters()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9
1
<?php
2
3
namespace Adminetic\Website\Http\Livewire\Admin\Attribute;
4
5
use Adminetic\Website\Models\Admin\Attribute;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Support\Facades\Blade;
8
use Rappasoft\LaravelLivewireTables\DataTableComponent;
9
use Rappasoft\LaravelLivewireTables\Views\Column;
10
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
11
12
class AttributeTable extends DataTableComponent
13
{
14
    public function builder(): Builder
15
    {
16
        return Attribute::query()->orderBy('position'); // Eager load anything; // Select some things
0 ignored issues
show
Bug Best Practice introduced by
The expression return Adminetic\Website...()->orderBy('position') could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
17
    }
18
19
    public array $bulkActions = [
20
        'bulkDelete' => 'Bulk Delete',
21
    ];
22
23
    public function filters(): array
24
    {
25
        return [
26
            SelectFilter::make('Searchable')
27
                ->options([
28
                    '' => 'All',
29
                    '1' => 'Yes',
30
                    '0' => 'No',
31
                ])
32
                ->filter(function (Builder $builder, string $value) {
33
                    if ($value === '1') {
34
                        $builder->where('is_searchable', true);
35
                    } elseif ($value === '0') {
36
                        $builder->where('is_searchable', false);
37
                    }
38
                }),
39
        ];
40
    }
41
42
    public function bulkDelete()
43
    {
44
        Attribute::whereIn('id', $this->getSelected())->delete();
45
    }
46
47
    public function configure(): void
48
    {
49
        $this->setPrimaryKey('id');
50
51
        $this->setEagerLoadAllRelationsEnabled();
52
53
        $this->setEmptyMessage('No attribute found');
54
55
        $this->setReorderStatus(true);
56
57
        $this->setDefaultReorderSort('position', 'asc');
58
    }
59
60
    public function reorder($items): void
61
    {
62
        foreach ($items as $item) {
63
            Attribute::find((int) $item['value'])->update(['position' => (int) $item['order']]);
64
        }
65
    }
66
67
    public function columns(): array
68
    {
69
        return [
70
            Column::make('ID', 'id')
71
                ->sortable()
72
                ->searchable(),
73
            Column::make('Name', 'name')
74
                ->sortable()
75
                ->searchable(),
76
            Column::make('Values', 'values')
77
                ->format(
78
                    fn ($value, $row, Column $column) => view('website::admin.layouts.modules.attribute.values', ['attribute' => $row])
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

78
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => view('website::admin.layouts.modules.attribute.values', ['attribute' => $row])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
                )
80
                ->collapseOnTablet(),
81
            Column::make('Searchable', 'is_searchable')
82
                ->format(
83
                    fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('is_searchable') ? 'success' : 'danger').' ">'.($row->getRawOriginal('is_searchable') ? 'Yes' : 'No').'</span>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

83
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="badge badge-'.($row->getRawOriginal('is_searchable') ? 'success' : 'danger').' ">'.($row->getRawOriginal('is_searchable') ? 'Yes' : 'No').'</span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
                )
85
                ->html()
86
                ->collapseOnTablet(),
87
            Column::make('Action')
88
                ->label(
89
                    fn ($row, Column $column) => Blade::render('<x-adminetic-action :model="$model" route="attribute" :show="0" />', ['model' => $row])
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

89
                    fn ($row, /** @scrutinizer ignore-unused */ Column $column) => Blade::render('<x-adminetic-action :model="$model" route="attribute" :show="0" />', ['model' => $row])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
                )
91
                ->html()
92
                ->collapseOnTablet(),
93
        ];
94
    }
95
}
96