Workflow
Lazy Load

This example enables Lazy Loading.

ID
Name
Category
Price
In Stock
Created At
1
Arkansas Possum Pie
Garnish
€243.39
Yes
06/07/2024
2
Albacore Tuna Melt
Pie
€63.11
Yes
03/07/2024
3
борщ
Soup
€100.19
No
26/06/2024
4
Bacalhau com natas
Garnish
€238.11
No
02/06/2024
5
Baba Ghanoush
Pasta
€143.09
No
30/05/2024
6
Bacon Cheeseburger
Fish
€257.39
Yes
13/06/2024
7
Baked potato
Garnish
€171.89
No
05/07/2024
8
Baklava
Dessert
€143.46
No
14/07/2024
9
Bangers and mash
Dessert
€253.72
Yes
26/05/2024
10
Black Pudding
Pasta
€248.73
No
10/07/2024
11
Blue cheese dressing
Pasta
€130.90
No
25/05/2024
12
Boulliabaise
Soup
€104.68
No
21/06/2024
13
Bread
Garnish
€60.60
No
18/06/2024
14
Breaded shrimp
Pie
€186.47
No
31/05/2024
15
Breakfast burrito
Pasta
€199.36
No
18/05/2024
16
Brisket
Soup
€138.11
No
25/06/2024
17
Brunswick stew
Meat
€165.15
No
01/07/2024
18
Bruschetta
Pasta
€165.87
No
28/05/2024
19
Buffalo Wings
Garnish
€86.31
No
16/07/2024
20
Buffalo burger
Pie
€278.57
No
23/06/2024
21
Buffalo wing
Soup
€82.90
No
08/07/2024
22
Calamari
Fish
€252.14
No
28/05/2024
23
Carne pizzaiola
Garnish
€170.92
No
04/06/2024
24
Caviar
Meat
€164.59
No
25/05/2024
25
Ceviche
Pie
€99.14
No
09/07/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\Footer;
use PowerComponents\LivewirePowerGrid\Header;
use PowerComponents\LivewirePowerGrid\Lazy;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
class LazyLoadTable extends PowerGridComponent
{
public function setUp(): array
{
return [
Header::make()
->showSearchInput(),
 
Footer::make()
->showPerPage(200)
->showRecordCount(),
 
Lazy::make()
->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

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

Name
Version
Description
laravel/framework
v11
The Laravel Framework.
livewire/livewire
v3.5.4
A front-end framework for Laravel.
openspout/openspout
v4.24.3
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.