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
29/05/2024 11:50:08
2
Luan
luanfreitasdev@fakemail.com
03/07/2024 10:57:00
3
Daniel
dansysanalyst@fakemail.com
03/07/2024 10:57:00
4
Claudio
claudio@fakemail.com
03/07/2024 10:57:00
5
Vitor
vitao@fakemail.com
03/07/2024 10:57:00
6
Tio Jobs
tiojobs@fakemail.com
03/07/2024 10:57:00
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.5.0
A front-end framework for Laravel.
openspout/openspout
v4.24.1
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.