1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Adminetic\Website\Http\Livewire\Admin\Post; |
4
|
|
|
|
5
|
|
|
use Adminetic\Website\Models\Admin\Post; |
6
|
|
|
use App\Models\User; |
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Illuminate\Support\Facades\Blade; |
10
|
|
|
use Rappasoft\LaravelLivewireTables\DataTableComponent; |
11
|
|
|
use Rappasoft\LaravelLivewireTables\Views\Column; |
12
|
|
|
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter; |
13
|
|
|
|
14
|
|
|
class PostTable extends DataTableComponent |
15
|
|
|
{ |
16
|
|
|
public function builder(): Builder |
17
|
|
|
{ |
18
|
|
|
return Post::query() |
|
|
|
|
19
|
|
|
->with('category')->orderBy('position'); // Eager load anything; // Select some things |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function bulkActions(): array |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
|
|
'bulkDelete' => 'Bulk Delete', |
26
|
|
|
'bulkChangeStatusToNotApproved' => 'Mark as not approved', |
27
|
|
|
'bulkChangeStatusToPublish' => 'Mark as published', |
28
|
|
|
'bulkChangeStatusToPending' => 'Mark as pending', |
29
|
|
|
'bulkChangeStatusToDraft' => 'Mark as draft', |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function bulkDelete() |
34
|
|
|
{ |
35
|
|
|
Post::whereIn('id', $this->getSelected())->delete(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function bulkChangeStatusToNotApproved() |
39
|
|
|
{ |
40
|
|
|
Post::whereIn('id', $this->getSelected())->update([ |
41
|
|
|
'status' => 0, |
42
|
|
|
'approved_by' => Auth::user()->id, |
|
|
|
|
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function bulkChangeStatusToPublish() |
47
|
|
|
{ |
48
|
|
|
Post::whereIn('id', $this->getSelected())->update([ |
49
|
|
|
'status' => 1, |
50
|
|
|
'approved_by' => Auth::user()->id, |
|
|
|
|
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function bulkChangeStatusToPending() |
55
|
|
|
{ |
56
|
|
|
Post::whereIn('id', $this->getSelected())->update([ |
57
|
|
|
'status' => 2, |
58
|
|
|
'approved_by' => Auth::user()->id, |
|
|
|
|
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function bulkChangeStatusToDraft() |
63
|
|
|
{ |
64
|
|
|
Post::whereIn('id', $this->getSelected())->update([ |
65
|
|
|
'status' => 3, |
66
|
|
|
'approved_by' => Auth::user()->id, |
|
|
|
|
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function filters(): array |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
SelectFilter::make('Active') |
74
|
|
|
->options([ |
75
|
|
|
'' => 'All', |
76
|
|
|
'1' => 'Active', |
77
|
|
|
'0' => 'Inactive', |
78
|
|
|
]) |
79
|
|
|
->filter(function (Builder $builder, string $value) { |
80
|
|
|
if ($value === '1') { |
81
|
|
|
$builder->where('active', true); |
82
|
|
|
} elseif ($value === '0') { |
83
|
|
|
$builder->where('active', false); |
84
|
|
|
} |
85
|
|
|
}), |
86
|
|
|
SelectFilter::make('Featured') |
87
|
|
|
->options([ |
88
|
|
|
'' => 'All', |
89
|
|
|
'1' => 'Featured', |
90
|
|
|
'0' => 'Not Featured', |
91
|
|
|
]) |
92
|
|
|
->filter(function (Builder $builder, string $value) { |
93
|
|
|
if ($value === '1') { |
94
|
|
|
$builder->where('featured', true); |
95
|
|
|
} elseif ($value === '0') { |
96
|
|
|
$builder->where('featured', false); |
97
|
|
|
} |
98
|
|
|
}), |
99
|
|
|
SelectFilter::make('Status') |
100
|
|
|
->options([ |
101
|
|
|
'' => 'All', |
102
|
|
|
'0' => 'Not Approved', |
103
|
|
|
'1' => 'Published', |
104
|
|
|
'2' => 'Pending', |
105
|
|
|
'3' => 'Draft', |
106
|
|
|
]) |
107
|
|
|
->filter(function (Builder $builder, string $value) { |
108
|
|
|
if ($value === '0') { |
109
|
|
|
$builder->where('status', 0); |
110
|
|
|
} elseif ($value === '1') { |
111
|
|
|
$builder->where('status', 1); |
112
|
|
|
} elseif ($value === '2') { |
113
|
|
|
$builder->where('status', 2); |
114
|
|
|
} elseif ($value === '3') { |
115
|
|
|
$builder->where('status', 3); |
116
|
|
|
} |
117
|
|
|
}), |
118
|
|
|
SelectFilter::make('Author') |
119
|
|
|
->options(array_merge([ |
120
|
|
|
'' => 'All', |
121
|
|
|
], User::find(array_unique(Post::pluck('id')->toArray()))->mapWithKeys(function ($user) { |
122
|
|
|
return [$user->id => $user->name]; |
123
|
|
|
})->toArray())) |
124
|
|
|
->filter(function (Builder $builder, string $value) { |
125
|
|
|
$builder->where('user_id', (int) $value); |
126
|
|
|
}), |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function configure(): void |
131
|
|
|
{ |
132
|
|
|
$this->setPrimaryKey('id'); |
133
|
|
|
|
134
|
|
|
$this->setEagerLoadAllRelationsEnabled(); |
135
|
|
|
|
136
|
|
|
$this->setEmptyMessage('No post found'); |
137
|
|
|
|
138
|
|
|
$this->setReorderStatus(true); |
139
|
|
|
|
140
|
|
|
$this->setBulkActionsStatus(true); |
141
|
|
|
|
142
|
|
|
$this->setBulkActions([ |
143
|
|
|
'exportSelected' => 'Export', |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
$this->setDefaultReorderSort('position', 'asc'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function reorder($items): void |
150
|
|
|
{ |
151
|
|
|
foreach ($items as $item) { |
152
|
|
|
Post::find((int) $item['value'])->update(['position' => (int) $item['order']]); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function columns(): array |
157
|
|
|
{ |
158
|
|
|
return [ |
159
|
|
|
Column::make('ID', 'id') |
160
|
|
|
->sortable() |
161
|
|
|
->searchable(), |
162
|
|
|
Column::make('Name', 'name') |
163
|
|
|
->sortable() |
164
|
|
|
->searchable(), |
165
|
|
|
Column::make('Category', 'category_id') |
166
|
|
|
->format( |
167
|
|
|
fn ($value, $row, Column $column) => $row->category->name ?? '-' |
|
|
|
|
168
|
|
|
) |
169
|
|
|
->sortable() |
170
|
|
|
->searchable() |
171
|
|
|
->collapseOnTablet(), |
172
|
|
|
Column::make('Active', 'active') |
173
|
|
|
->format( |
174
|
|
|
fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'danger').' ">'.($row->getRawOriginal('active') ? 'Active' : 'Inactive').'</span>' |
|
|
|
|
175
|
|
|
) |
176
|
|
|
->html() |
177
|
|
|
->collapseOnTablet(), |
178
|
|
|
Column::make('Featured', 'featured') |
179
|
|
|
->format( |
180
|
|
|
fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('featured') ? 'success' : 'primary').' ">'.($row->getRawOriginal('featured') ? 'Featured' : 'Not Featured').'</span>' |
|
|
|
|
181
|
|
|
) |
182
|
|
|
->html() |
183
|
|
|
->collapseOnTablet(), |
184
|
|
|
Column::make('Status', 'status') |
185
|
|
|
->format( |
186
|
|
|
fn ($value, $row, Column $column) => '<span class="badge badge-'.$row->getStatusColor().' ">'.$row->status.'</span>' |
|
|
|
|
187
|
|
|
) |
188
|
|
|
->html() |
189
|
|
|
->collapseOnTablet(), |
190
|
|
|
Column::make('Action') |
191
|
|
|
->label( |
192
|
|
|
fn ($row, Column $column) => Blade::render('<x-adminetic-action :model="$model" route="post" />', ['model' => $row]) |
|
|
|
|
193
|
|
|
) |
194
|
|
|
->html() |
195
|
|
|
->collapseOnTablet(), |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths