Workflow
Datasource Query Builder

This example demonstrates a Query Builder Datasource.

XLSX
Csv
ID
Name
Email
Active
Created at
1
Test User
test@example.com
18/05/2024 12:58:29
2
Luan
luanfreitasdev@fakemail.com
20/05/2024 18:11:47
3
Daniel
dansysanalyst@fakemail.com
20/05/2024 18:11:47
4
Claudio
claudio@fakemail.com
20/05/2024 18:11:47
5
Vitor
vitao@fakemail.com
20/05/2024 18:11:47
6
Tio Jobs
tiojobs@fakemail.com
20/05/2024 18:11:47
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\DatasourceQueryBuilderTable;
 
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Exportable;
use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Footer;
use PowerComponents\LivewirePowerGrid\Header;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
use PowerComponents\LivewirePowerGrid\Traits\WithExport;
 
final class DatasourceQueryBuilderTable extends PowerGridComponent
{
use WithExport;
 
public function setUp(): array
{
$this->showCheckBox();
 
return [
Exportable::make('export')
->striped()
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV),
 
Header::make()->showSearchInput(),
 
Footer::make()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource(): ?Builder
{
return DB::table('users');
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('active')
->add('created_at_formatted', fn ($model) => Carbon::parse($model->created_at)->format('d/m/Y H:i:s'))
->add('email')
->add('id')
->add('name');
}
 
public function columns(): array
{
return [
Column::make('ID', 'id'),
 
Column::make('Name', 'name')
->sortable()
->searchable(),
 
Column::make('Email', 'email')
->sortable()
->searchable(),
 
Column::make('Active', 'active')
->toggleable(),
 
Column::make('Created at', 'created_at_formatted', 'created_at')
->sortable(),
 
];
}
 
public function filters(): array
{
return [
Filter::boolean('active'),
 
Filter::datetimepicker('created_at'),
 
Filter::inputText('email')->operators(['contains']),
 
Filter::inputText('name')->operators(['contains']),
];
}
}
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.4.12
A front-end framework for Laravel.
openspout/openspout
v4.24.0
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
power-components/livewire-powergrid
v5.6.3
PowerGrid generates Advanced Datatables using Laravel Livewire.