Workflow
Lazy Load

This example enables Lazy Loading.

ID
Name
Category
Price
In Stock
Created At
1
Arkansas Possum Pie
Soup
€118.08
No
26/09/2024
2
Albacore Tuna Melt
Pasta
€132.75
No
07/10/2024
3
борщ
Soup
€100.19
No
26/09/2024
4
Bacalhau com natas
Dessert
€208.60
No
29/09/2024
5
Baba Ghanoush
Soup
€183.81
Yes
03/10/2024
6
Bacon Cheeseburger
Soup
€137.48
Yes
11/09/2024
7
Baked potato
Pasta
€189.66
Yes
03/10/2024
8
Baklava
Pie
€161.92
Yes
12/10/2024
9
Bangers and mash
Pie
€214.04
Yes
03/10/2024
10
Black Pudding
Pasta
€116.13
Yes
03/10/2024
11
Blue cheese dressing
Meat
€121.08
Yes
21/08/2024
12
Boulliabaise
Soup
€199.67
Yes
30/09/2024
13
Bread
Fish
€161.33
No
10/09/2024
14
Breaded shrimp
Soup
€251.46
No
19/09/2024
15
Breakfast burrito
Dessert
€269.87
No
20/09/2024
16
Brisket
Soup
€212.26
No
15/09/2024
17
Brunswick stew
Pasta
€157.42
No
11/10/2024
18
Bruschetta
Pie
€61.54
Yes
21/08/2024
19
Buffalo Wings
Dessert
€163.17
No
20/09/2024
20
Buffalo burger
Soup
€150.65
Yes
21/08/2024
21
Buffalo wing
Pasta
€126.61
Yes
17/08/2024
22
Calamari
Pasta
€158.29
No
06/10/2024
23
Carne pizzaiola
Meat
€224.92
Yes
27/08/2024
24
Caviar
Meat
€170.37
No
10/09/2024
25
Ceviche
Soup
€54.60
No
10/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