Workflow
Lazy Load

This example enables Lazy Loading.

ID
Name
Category
Price
In Stock
Created At
1
Arkansas Possum Pie
Garnish
€129.15
Yes
08/09/2024
2
Albacore Tuna Melt
Fish
€142.76
No
23/08/2024
3
борщ
Soup
€100.19
Yes
17/08/2024
4
Bacalhau com natas
Dessert
€158.11
No
26/08/2024
5
Baba Ghanoush
Garnish
€130.67
Yes
11/08/2024
6
Bacon Cheeseburger
Pasta
€53.09
Yes
16/08/2024
7
Baked potato
Pie
€261.66
Yes
26/08/2024
8
Baklava
Meat
€159.65
Yes
01/10/2024
9
Bangers and mash
Garnish
€135.15
No
15/08/2024
10
Black Pudding
Pie
€228.87
No
14/08/2024
11
Blue cheese dressing
Fish
€266.71
No
15/08/2024
12
Boulliabaise
Soup
€174.58
Yes
02/10/2024
13
Bread
Fish
€75.50
Yes
19/09/2024
14
Breaded shrimp
Soup
€276.36
Yes
03/08/2024
15
Breakfast burrito
Pasta
€210.23
Yes
08/08/2024
16
Brisket
Pie
€244.10
Yes
07/09/2024
17
Brunswick stew
Meat
€149.34
Yes
25/08/2024
18
Bruschetta
Pasta
€268.00
No
03/09/2024
19
Buffalo Wings
Dessert
€117.01
No
05/08/2024
20
Buffalo burger
Meat
€128.78
No
21/08/2024
21
Buffalo wing
Fish
€116.56
Yes
24/08/2024
22
Calamari
Garnish
€78.97
Yes
04/08/2024
23
Carne pizzaiola
Soup
€54.89
Yes
20/08/2024
24
Caviar
Garnish
€120.03
No
11/09/2024
25
Ceviche
Pasta
€181.81
Yes
30/09/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.
<?php
 
namespace App\Livewire\Examples\LazyLoadTable;
 
use App\Models\Dish;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Number;
use Livewire\Attributes\On;
use PowerComponents\LivewirePowerGrid\Column;
 
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
class LazyLoadTable extends PowerGridComponent
{
public string $tableName = 'lazy-load-table';
 
public function setUp(): array
{
return [
PowerGrid::header()
->showSearchInput(),
 
PowerGrid::footer()
->showPerPage(200)
->showRecordCount(),
 
PowerGrid::lazy()
->rowsPerChildren(25),
];
}
 
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', fn ($dish) => intval($dish->category_id))
->add('category_name', fn ($dish) => e($dish->category_name))
->add('price_in_eur', fn ($dish) => Number::currency($dish->price, in: 'EUR'))
->add('in_stock', fn ($dish) => $dish->in_stock ? 'Yes' : 'No')
->add('created_at_formatted', fn ($dish) => Carbon::parse($dish->created_at)->format('d/m/Y'));
}
 
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_in_eur', 'price')
->searchable()
->sortable(),
 
Column::make('In Stock', 'in_stock'),
 
Column::make('Created At', 'created_at_formatted'),
];
}
 
#[On('edit')]
public function edit(int $dishId): void
{
$this->js('alert(' . $dishId . ')');
}
}
Code highlighting provided by Torchlight.dev