CategoryTable::bulkDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Adminetic\Website\Http\Livewire\Admin\Category;
4
5
use Adminetic\Website\Models\Admin\Category;
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 CategoryTable extends DataTableComponent
13
{
14
    public function builder(): Builder
15
    {
16
        return Category::query()
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
            ->with('parent', 'root_parent')->orderBy('position'); // Eager load anything; // Select some things
18
    }
19
20
    public array $bulkActions = [
21
        'bulkDelete' => 'Bulk Delete',
22
    ];
23
24
    public function bulkDelete()
25
    {
26
        Category::whereIn('id', $this->getSelected())->delete();
27
    }
28
29
    public function filters(): array
30
    {
31
        return [
32
            SelectFilter::make('Active')
33
                ->options([
34
                    '' => 'All',
35
                    '1' => 'Active',
36
                    '0' => 'Inactive',
37
                ])
38
                ->filter(function (Builder $builder, string $value) {
39
                    if ($value === '1') {
40
                        $builder->where('active', true);
41
                    } elseif ($value === '0') {
42
                        $builder->where('active', false);
43
                    }
44
                }),
45
            SelectFilter::make('Featured')
46
                ->options([
47
                    '' => 'All',
48
                    '1' => 'Featured',
49
                    '0' => 'Not Featured',
50
                ])
51
                ->filter(function (Builder $builder, string $value) {
52
                    if ($value === '1') {
53
                        $builder->where('featured', true);
54
                    } elseif ($value === '0') {
55
                        $builder->where('featured', false);
56
                    }
57
                }),
58
        ];
59
    }
60
61
    public function configure(): void
62
    {
63
        $this->setPrimaryKey('id');
64
65
        $this->setEagerLoadAllRelationsEnabled();
66
67
        $this->setEmptyMessage('No category found');
68
69
        $this->setReorderStatus(true);
70
71
        $this->setDefaultReorderSort('position', 'asc');
72
    }
73
74
    public function reorder($items): void
75
    {
76
        foreach ($items as $item) {
77
            Category::find((int) $item['value'])->update(['position' => (int) $item['order']]);
78
        }
79
    }
80
81
    public function columns(): array
82
    {
83
        return [
84
            Column::make('ID', 'id')
85
                ->sortable()
86
                ->searchable(),
87
            Column::make('Name', 'name')
88
                ->sortable()
89
                ->searchable(),
90
            Column::make('Parent', 'parent_id')
91
                ->format(
92
                    fn ($value, $row, Column $column) => $row->parent->name ?? '-'
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

92
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => $row->parent->name ?? '-'

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...
93
                )
94
                ->sortable()
95
                ->searchable()
96
                ->collapseOnTablet(),
97
            Column::make('Root Parent', 'root_parent_id')
98
                ->format(
99
                    fn ($value, $row, Column $column) => $row->root_parent->name ?? '-'
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

99
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => $row->root_parent->name ?? '-'

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...
100
                )
101
                ->sortable()
102
                ->searchable()
103
                ->collapseOnTablet(),
104
            Column::make('Model', 'model')
105
                ->sortable()
106
                ->collapseOnTablet(),
107
            Column::make('Active', 'active')
108
                ->format(
109
                    fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'danger').' ">'.($row->getRawOriginal('active') ? 'Active' : 'Inactive').'</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

109
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'danger').' ">'.($row->getRawOriginal('active') ? 'Active' : 'Inactive').'</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...
110
                )
111
                ->html()
112
                ->collapseOnTablet(),
113
            Column::make('Featured', 'featured')
114
                ->format(
115
                    fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'primary').' ">'.($row->getRawOriginal('active') ? 'Featured' : 'Not Featured').'</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

115
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'primary').' ">'.($row->getRawOriginal('active') ? 'Featured' : 'Not Featured').'</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...
116
                )
117
                ->html()
118
                ->collapseOnTablet(),
119
            Column::make('Icon', 'icon')
120
                ->format(
121
                    fn ($value, $row, Column $column) => '<span class="'.$row->icon.' "></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

121
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="'.$row->icon.' "></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...
122
                )
123
                ->html()
124
                ->collapseOnTablet(),
125
            Column::make('Color', 'color')
126
                ->format(
127
                    fn ($value, $row, Column $column) => '<span style="height:30px;width:50px;background-color:'.$row->color.'"></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

127
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span style="height:30px;width:50px;background-color:'.$row->color.'"></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...
128
                )
129
                ->html()
130
                ->collapseOnTablet(),
131
            Column::make('Action')
132
                ->label(
133
                    fn ($row, Column $column) => Blade::render('<x-adminetic-action :model="$model" route="category" :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

133
                    fn ($row, /** @scrutinizer ignore-unused */ Column $column) => Blade::render('<x-adminetic-action :model="$model" route="category" :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...
134
                )
135
                ->html()
136
                ->collapseOnTablet(),
137
        ];
138
    }
139
}
140