91 lines
3.4 KiB
PHP
91 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Datatable\Studio\Catalog;
|
|
|
|
use App\Models\Bottle;
|
|
use App\Traits\Datatable\WithConfiguration;
|
|
use App\Traits\Datatable\WithPrependColumn;
|
|
use App\Traits\WithMediaHandler;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Blade;
|
|
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
|
use Rappasoft\LaravelLivewireTables\Views\Column;
|
|
use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn;
|
|
use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;
|
|
|
|
class BottlesTable extends DataTableComponent
|
|
{
|
|
use WithConfiguration, WithMediaHandler, WithPrependColumn;
|
|
|
|
protected $model = Bottle::class;
|
|
|
|
public function columns(): array
|
|
{
|
|
return [
|
|
Column::make('Nama')
|
|
->label(function ($row) {
|
|
return <<<HTML
|
|
<div>
|
|
<div>{$row->name}</div>
|
|
<span class="text-xs text-gray-400">{$row->size} ml</span>
|
|
</div>
|
|
HTML;
|
|
})
|
|
->searchable(function ($query, $searchTerm) {
|
|
$query->where('name', 'like', "%{$searchTerm}%")
|
|
->orWhere('size', 'like', "%{$searchTerm}%");
|
|
})
|
|
->html(),
|
|
|
|
Column::make('Harga Beli', 'cost_price')
|
|
->label(fn ($row, $column) => currency($row->cost_price, 'Rp'))
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
Column::make('Harga Jual', 'sale_price')
|
|
->label(fn ($row, $column) => currency($row->sale_price, 'Rp'))
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
ImageColumn::make('Gambar')
|
|
->location(fn ($row) => optional($row->getMedia('image')->first())->getUrl() ?? asset('assets/images/logo.png'))
|
|
->attributes(fn ($row) => [
|
|
'style' => 'width: 50px; height: 50px;',
|
|
'alt' => $row->name,
|
|
]),
|
|
|
|
ArrayColumn::make('Outlet')
|
|
->data(fn ($value, $row) => $row->outlets->pluck('name')->toArray())
|
|
->outputFormat(fn ($index, $value) => Blade::render('<span class="text-xs text-gray-400 border border-gray-400 rounded px-1">'.$value.'</span>'))
|
|
->flexRow(['class' => 'gap-2 flex-wrap']),
|
|
|
|
Column::make('Aksi')
|
|
->label(function ($row) {
|
|
$actions = '';
|
|
|
|
if (auth()->user()->can('update bottle')) {
|
|
$actions .= view('components.datatables.edit', [
|
|
'id' => $row->id,
|
|
'editRoute' => route('studio.catalog.bottle.edit', $row->id),
|
|
])->render();
|
|
}
|
|
|
|
if (auth()->user()->can('delete bottle')) {
|
|
$actions .= view('components.datatables.delete', [
|
|
'id' => $row->id,
|
|
'deleteRoute' => route('studio.catalog.bottle.delete', $row->id),
|
|
])->render();
|
|
}
|
|
|
|
return $actions;
|
|
})
|
|
->html(),
|
|
];
|
|
}
|
|
|
|
public function builder(): Builder
|
|
{
|
|
return Bottle::select('id', 'name', 'size', 'cost_price', 'sale_price')->with('outlets');
|
|
}
|
|
}
|