Workflow
Bulk Action
ID
Dish
Chef
Diet
Category
Price
Sales price
Calories
In Stock
Kitchen
Production date
1
Arkansas Possum Pie
-
πŸ₯œ Suitable for Celiacs
Pie
R$ 109,25
849 kcal
RJ
28/03/2024
2
Albacore Tuna Melt
-
πŸ₯œ Suitable for Celiacs
Dessert
R$ 239,20
244 kcal
RJ
15/03/2024
3
Π±ΠΎΡ€Ρ‰
Dan
🌱 Suitable for Vegans
Fish
R$ 318,55
799 kcal
BA
17/03/2024
4
Bacalhau com natas
-
πŸ₯œ Suitable for Celiacs
Garnish
R$ 171,35
749 kcal
MG
01/04/2024
5
Baba Ghanoush
-
🌱 Suitable for Vegans
Pasta
R$ 64,40
692 kcal
SP
01/04/2024
6
Bacon Cheeseburger
-
🌱 Suitable for Vegans
Pie
R$ 227,70
657 kcal
RJ
02/04/2024
7
Baked potato
-
🌱 Suitable for Vegans
Fish
R$ 236,90
764 kcal
RJ
06/03/2024
8
Baklava
-
🌱 Suitable for Vegans
Dessert
R$ 77,05
768 kcal
MG
30/03/2024
9
Bangers and mash
-
🍽️ All diets
Meat
R$ 90,85
664 kcal
RJ
10/03/2024
10
Black Pudding
-
πŸ₯œ Suitable for Celiacs
Dessert
R$ 124,20
475 kcal
MG
13/03/2024
Disclaimer: Table data is randomly generated for illustrative purposes only. The information here is not a reflection of the actual market and does not constitute business, financial, or medical advice.
open in GitHub
<?php
 
use App\Enums\Diet;
use App\Models\Dish;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\On;
use PowerComponents\LivewirePowerGrid\Button;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Footer;
use PowerComponents\LivewirePowerGrid\Header;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
final class BulkActionTable extends PowerGridComponent
{
public string $sortField = 'dishes.id';
 
#[On('bulkDelete.{tableName}')]
public function bulkDelete(): void
{
$this->js('alert(window.pgBulkActions.get(\''.$this->tableName.'\'))');
}
 
public function editDish(array $data): void
{
dd('You are editing', $data);
}
 
public function setUp(): array
{
$this->showCheckBox();
 
return [
Header::make()
->showSearchInput(),
 
Footer::make()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource()
{
return Dish::query()
->leftJoin('categories', function ($categories) {
$categories->on('dishes.category_id', '=', 'categories.id');
})
->leftJoin('kitchens', function ($categories) {
$categories->on('dishes.kitchen_id', '=', 'kitchens.id');
})
->leftJoin('chefs', function ($categories) {
$categories->on('dishes.chef_id', '=', 'chefs.id');
})
->select('dishes.*', 'categories.name as category_name', DB::raw('DATE_FORMAT(dishes.produced_at, "%d/%m/%Y") as produced_at_formatted'));
}
 
public function relationSearch(): array
{
return [
'category' => [
'name',
],
];
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('serving_at')
->add('chef_name', function (Dish $dish) {
return $dish->chef?->name ?? '-';
})
->add('dish_name', function (Dish $dish) {
return $dish->name;
})
->add('calories', function (Dish $dish) {
return $dish->calories.' kcal';
})
->add('category_id', function (Dish $dish) {
return $dish->category_id;
})
->add('category.name')
->add('kitchen_id', function (Dish $dish) {
return $dish->kitchen_id;
})
->add('kitchen.description')
->add('price')
->add('price_BRL', function (Dish $dish) {
return 'R$ '.number_format($dish->price, 2, ',', '.'); //R$ 1.000,00
})
->add('sales_price')
->add('sales_price_BRL', function (Dish $dish) {
$sales_price = $dish->price + ($dish->price * 0.15);
 
return 'R$ '.number_format($sales_price, 2, ',', '.'); //R$ 1.000,00
})
->add('in_stock')
->add('in_stock_label', function (Dish $dish) {
return $dish->in_stock ? 'yes' : 'no';
})
->add('diet', function (Dish $dish) {
return Diet::from($dish->diet)->labels();
})
->add('produced_at')
->add('produced_at_formatted');
}
 
public function columns(): array
{
return [
Column::add()
->title(__('ID'))
->field('id', 'dishes.id')
->searchable()
->sortable(),
 
Column::add()
->title(__('Dish'))
->field('dish_name', 'dishes.name')
->searchable()
->editOnClick(true)
->placeholder('Dish placeholder')
->sortable(),
 
Column::add()
->title(__('Chef'))
->field('chef_name', 'chefs.name')
->searchable()
->placeholder('Chef placeholder')
->sortable(),
 
Column::add()
->field('diet', 'dishes.diet')
->title(__('Diet')),
 
Column::add()
->title(__('Category'))
->field('category.name')
->placeholder('Category placeholder')
->sortable(),
 
Column::add()
->title(__('Price'))
->field('price')
->editOnClick(true),
 
Column::add()
->title(__('Sales price'))
->field('sales_price_BRL'),
 
Column::add()
->title(__('Calories'))
->field('calories')
->sortable(),
 
Column::add()
->title(__('In Stock'))
->field('in_stock')
->toggleable(true, 'yes', 'no')
->sortable(),
 
Column::add()
->title(__('Kitchen'))
->field('kitchen.description')
->sortable(),
 
Column::add()
->title(__('Production date'))
->field('produced_at_formatted')
->sortable(),
// ->searchableRaw('DATE_FORMAT(dishes.produced_at, "%d/%m/%Y")'),
];
}
 
public function header(): array
{
return [
Button::add('bulk-delete')
->slot(__('Bulk delete (<span x-text="window.pgBulkActions.count(\''.$this->tableName.'\')"></span>)'))
->class('pg-btn-white dark:ring-pg-primary-600 dark:border-pg-primary-600 dark:hover:bg-pg-primary-700 dark:ring-offset-pg-primary-800 dark:text-pg-primary-300 dark:bg-pg-primary-700')
->dispatch('bulkDelete.'.$this->tableName, []),
];
}
 
public function filters(): array
{
return [
Filter::number('price')->thousands(','),
];
}
}

Here you can find all relevant packages installed on this demo.

Name
Version
Description
laravel/framework
v10
The Laravel Framework.
openspout/openspout
v4.23.0
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
power-components/livewire-powergrid
5.x-dev
PowerGrid generates Advanced Datatables using Laravel Livewire.