Workflow
Simple
ID
Name
Category
Price
In Stock
Created At
1
Arkansas Possum Pie
Pie
$95.00
Yes
05/04/2024
2
Albacore Tuna Melt
Dessert
$208.00
Yes
05/04/2024
3
борщ
Fish
$277.00
Yes
05/04/2024
4
Bacalhau com natas
Garnish
$149.00
Yes
05/04/2024
5
Baba Ghanoush
Pasta
$56.00
No
05/04/2024
6
Bacon Cheeseburger
Pie
$198.00
No
05/04/2024
7
Baked potato
Fish
$206.00
Yes
05/04/2024
8
Baklava
Dessert
$67.00
No
05/04/2024
9
Bangers and mash
Meat
$79.00
Yes
05/04/2024
10
Black Pudding
Dessert
$108.00
Yes
05/04/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\Models\Dish;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Footer;
use PowerComponents\LivewirePowerGrid\Header;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
class SimpleTable extends PowerGridComponent
{
public string $tableName = 'simpleTable';
 
public function setUp(): array
{
return [
Header::make()
->showSearchInput(),
 
Footer::make()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource(): ?Builder
{
return Dish::query()
->join('categories as newCategories', function ($categories) {
$categories->on('dishes.category_id', '=', 'newCategories.id');
})
->select('dishes.*', 'newCategories.name as category_name')
->toBase();
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('category_id', function ($dish) {
return $dish->category_id;
})
->add('category_name', function ($dish) {
return $dish->category_name;
})
->add('price_fmt', function ($dish) {
return (new \NumberFormatter('en_US', \NumberFormatter::CURRENCY))
->formatCurrency($dish->price, 'USD');
})
->add('in_stock', function ($dish) {
return $dish->in_stock ? 'Yes' : 'No';
})
->add('created_at_formatted', function ($dish) {
return Carbon::parse($dish->created_at)
->timezone('America/Sao_Paulo')->format('d/m/Y');
});
}
 
public function beforeSearch(?string $field = null, ?string $search = null): ?string
{
if ($field === 'in_stock') {
return str(strtolower($search))
->replace('no', '0')
->replace('yes', '1')
->toString();
}
 
return $search;
}
 
public function columns(): array
{
return [
 
Column::make('ID', 'id')
->searchable()
->sortable(),
 
Column::make('Name', 'name')
->bodyAttribute('!text-wrap')
->searchable()
->sortable(),
 
Column::make('Category', 'category_name'),
 
Column::make('Price', 'price_fmt', 'price')
->searchable()
->sortable(),
 
Column::make('In Stock', 'in_stock')
->searchable(),
 
Column::make('Created At', 'created_at_formatted'),
];
}
 
#[\Livewire\Attributes\On('edit')]
public function edit(int $dishId): void
{
$this->js('alert('.$dishId.')');
}
}

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.