Workflow
Lazy Load

This example enables Lazy Loading.

ID
Name
Category
Price
In Stock
Created At
1
Arkansas Possum Pie
Pie
€188.03
Yes
03/11/2024
2
Albacore Tuna Melt
Soup
€145.93
Yes
01/12/2024
3
борщ
Soup
€100.19
Yes
02/12/2024
4
Bacalhau com natas
Garnish
€115.28
No
22/11/2024
5
Baba Ghanoush
Soup
€127.86
No
02/11/2024
6
Bacon Cheeseburger
Pasta
€220.97
Yes
04/11/2024
7
Baked potato
Dessert
€195.18
Yes
28/10/2024
8
Baklava
Pasta
€254.15
Yes
06/12/2024
9
Bangers and mash
Soup
€172.21
Yes
11/12/2024
10
Black Pudding
Meat
€230.45
Yes
18/12/2024
11
Blue cheese dressing
Dessert
€240.58
Yes
25/10/2024
12
Boulliabaise
Dessert
€108.09
Yes
18/11/2024
13
Bread
Pie
€230.39
Yes
26/10/2024
14
Breaded shrimp
Dessert
€169.79
Yes
19/10/2024
15
Breakfast burrito
Meat
€166.15
Yes
16/12/2024
16
Brisket
Garnish
€276.97
Yes
24/10/2024
17
Brunswick stew
Meat
€253.14
Yes
06/12/2024
18
Bruschetta
Pasta
€260.61
No
20/10/2024
19
Buffalo Wings
Pie
€150.66
No
10/12/2024
20
Buffalo burger
Pasta
€174.57
No
07/11/2024
21
Buffalo wing
Dessert
€163.31
Yes
20/10/2024
22
Calamari
Pie
€52.25
Yes
30/11/2024
23
Carne pizzaiola
Dessert
€63.54
No
07/12/2024
24
Caviar
Garnish
€91.36
No
27/10/2024
25
Ceviche
Pie
€278.46
No
23/11/2024
Showing 1 to 192 of 192 Results
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 . ')');
    }
}