Workflow
Query Builder
XLSX
Csv
Id
Name
Email
Active
Created at
1
Luan
luanfreitasdev@fakemail.com
19/04/2024 08:58:15
2
Daniel
dansysanalyst@fakemail.com
19/04/2024 08:58:15
3
Claudio
claudio@fakemail.com
19/04/2024 08:58:15
4
Vitor
vitao@fakemail.com
19/04/2024 08:58:15
5
Tio Jobs
tiojobs@fakemail.com
19/04/2024 08:58:15
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.
open in GitHub
<?php
 
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Components\Rules\RuleActions;
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 QueryBuilderTable extends PowerGridComponent
{
use WithExport;
 
/*
|--------------------------------------------------------------------------
| Features Setup
|--------------------------------------------------------------------------
| Setup Table's general features
|
*/
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(),
];
}
 
/*
|--------------------------------------------------------------------------
| Datasource
|--------------------------------------------------------------------------
| Provides data to your Table using a Eloquent, Query Builder or Collection
|
*/
 
 
public function datasource(): Builder
{
return DB::table('users');
}
 
/*
|--------------------------------------------------------------------------
| Relationship Search
|--------------------------------------------------------------------------
| Configure here relationships to be used by the Search and Table Filters.
|
*/
 
/**
* Relationship search.
*
* @return array<string, array<int, string>>
*/
public function relationSearch(): array
{
return [];
}
 
/*
|--------------------------------------------------------------------------
| Add Column
|--------------------------------------------------------------------------
| Make Datasource fields available to be used as columns.
| You can pass a closure to transform/modify the data.
|
| ❗ IMPORTANT: When using closures, you must escape any value coming from
| the database using the `e()` Laravel Helper function.
|
*/
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');
}
 
/*
|--------------------------------------------------------------------------
| Include Columns
|--------------------------------------------------------------------------
| Include the columns added columns, making them visible on the Table.
| Each column can be configured with properties, filters, actions...
|
*/
 
/**
* PowerGrid Columns.
*
* @return array<int, Column>
*/
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(),
 
];
}
 
/**
* PowerGrid Filters.
*
* @return array<int, Filter>
*/
public function filters(): array
{
return [
Filter::boolean('active'),
Filter::datetimepicker('created_at'),
Filter::inputText('email')->operators(['contains']),
Filter::inputText('name')->operators(['contains']),
];
}
 
/*
|--------------------------------------------------------------------------
| Actions Method
|--------------------------------------------------------------------------
| Enable the method below only if the Routes below are defined in your app.
|
*/
 
/*
|--------------------------------------------------------------------------
| Actions Rules
|--------------------------------------------------------------------------
| Enable the method below to configure Rules for your Table and Action Buttons.
|
*/
 
/**
* PowerGrid Action Rules.
*
* @return array<int, RuleActions>
*/
 
/*
public function actionRules(): array
{
return [
 
//Hide button edit for ID 1
Rule::button('edit')
->when(fn($row) => $row->id === 1)
->hide(),
];
}
*/
}

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

Name
Version
Description
laravel/framework
v10
The Laravel Framework.
openspout/openspout
v4.23.0
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.