parfum/app/Livewire/Datatable/Information/FaqsTable.php

96 lines
3.7 KiB
PHP

<?php
namespace App\Livewire\Datatable\Information;
use App\Models\Faq;
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 FaqsTable extends DataTableComponent
{
use WithConfiguration, WithPrependColumn;
protected $model = Faq::class;
public function columns(): array
{
return [
Column::make('Pertanyaan', 'question')->searchable(),
Column::make('Aksi')
->label(function ($row) {
$actions = '';
$maxSortOrder = Faq::max('sort_order');
if (auth()->user()->can('update faq')) {
if ($row->sort_order > 1) {
$actions .= view('components.actions.table.sort-button', [
'id' => $row->hash,
'module' => 'faq',
'direction' => 'up',
'tooltip' => 'Naik',
'color' => 'cyan',
'icon' => 'arrow-up',
])->render();
$actions .= view('components.actions.table.sort-button', [
'id' => $row->hash,
'module' => 'faq',
'direction' => 'first',
'tooltip' => 'Pertama',
'color' => 'emerald',
'icon' => 'bars-arrow-up',
])->render();
}
if ($row->sort_order < $maxSortOrder) {
$actions .= view('components.actions.table.sort-button', [
'id' => $row->hash,
'module' => 'faq',
'direction' => 'down',
'tooltip' => 'Turun',
'color' => 'orange',
'icon' => 'arrow-down',
])->render();
$actions .= view('components.actions.table.sort-button', [
'id' => $row->hash,
'module' => 'faq',
'direction' => 'last',
'tooltip' => 'Terakhir',
'color' => 'rose',
'icon' => 'bars-arrow-down',
])->render();
}
}
if (auth()->user()->can('update faq')) {
$actions .= view('components.actions.table.edit-modal', [
'id' => $row->hash,
'method' => 'update',
'modalTitle' => 'Ubah FAQs',
])->render();
}
if (auth()->user()->can('delete faq')) {
$actions .= view('components.actions.table.delete', [
'id' => $row->hash,
])->render();
}
return $actions;
})
->html()
->hideIf(auth()->user()->cannot('update faq') && auth()->user()->cannot('delete faq')),
];
}
public function builder(): Builder
{
return Faq::select('id', 'question', 'sort_order')->orderBy('sort_order');
}
}