parfum/app/Livewire/Datatable/CategoriesTable.php
Yoga Pangestu c60bb2b09d feat(category): crud
-membuat tabel, form, skema db
-membuat factory untuk kebutuhan test
-membuat test untuk memastikan semuanya berjalan lancar
-membuat seeder untuk data awal
-membuat action datatable baru yaitu sort-button
2025-09-29 14:57:42 +07:00

90 lines
3.1 KiB
PHP

<?php
namespace App\Livewire\Datatable;
use App\Models\Category;
use App\Traits\Datatable\WithConfiguration;
use App\Traits\Datatable\WithPrependColumn;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
class CategoriesTable extends DataTableComponent
{
use WithConfiguration, WithPrependColumn;
protected $model = Category::class;
public function columns(): array
{
return [
Column::make('Nama', 'name')->searchable(),
Column::make('Urutan', 'sort_order')
->sortable()
->hideIf(false),
Column::make('Aksi')
->label(function ($row) {
$actions = '';
$maxSortOrder = Category::max('sort_order');
if ($row->sort_order > 1) {
$actions .= view('components.datatables.sort-button', [
'id' => $row->id,
'direction' => 'up',
'tooltip' => 'Naik',
'color' => 'cyan',
'icon' => 'arrow-up',
])->render();
$actions .= view('components.datatables.sort-button', [
'id' => $row->id,
'direction' => 'first',
'tooltip' => 'Pertama',
'color' => 'emerald',
'icon' => 'bars-arrow-up',
])->render();
}
if ($row->sort_order < $maxSortOrder) {
$actions .= view('components.datatables.sort-button', [
'id' => $row->id,
'direction' => 'down',
'tooltip' => 'Turun',
'color' => 'orange',
'icon' => 'arrow-down',
])->render();
$actions .= view('components.datatables.sort-button', [
'id' => $row->id,
'direction' => 'last',
'tooltip' => 'Terakhir',
'color' => 'rose',
'icon' => 'bars-arrow-down',
])->render();
}
$actions .= view('components.datatables.edit-modal', [
'id' => $row->id,
'method' => 'update',
'modalTitle' => 'Ubah Kategori',
])->render();
$actions .= view('components.datatables.delete', [
'id' => $row->id,
'deleteRoute' => route('studio.catalog.category.delete', $row->id),
])->render();
return $actions;
})
->html(),
];
}
public function builder(): Builder
{
return Category::select('id', 'name', 'sort_order')->orderBy('sort_order');
}
}