Workflow
Datasource Collection

This example uses a Collection as Datasource.

Index
ID
Name
Balance
Online
Created At
Index
ID
Name
Balance
Online
Created At
1
29
Luan
R$ 241,86
01/01/2023
2
57
Daniel
R$ 166,51
02/02/2023
3
93
Claudio
R$ 219,01
03/03/2023
4
104
Vitor
R$ 44,28
04/04/2023
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\DatasourceCollectionTable;
 
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Number;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
use PowerComponents\LivewirePowerGrid\Traits\WithExport;
 
final class DatasourceCollectionTable extends PowerGridComponent
{
public string $tableName = 'datasource-collection-table';
 
use WithExport;
 
public function setUp(): array
{
return [
PowerGrid::header()
->showToggleColumns()
->showSearchInput(),
 
PowerGrid::footer()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource(): Collection
{
return collect([
[
'id' => 29,
'name' => 'Luan',
'balance' => 241.86,
'is_online' => true,
'created_at' => '2023-01-01 00:00:00',
],
[
'id' => 57,
'name' => 'Daniel',
'balance' => 166.51,
'is_online' => true,
'created_at' => '2023-02-02 00:00:00',
],
[
'id' => 93,
'name' => 'Claudio',
'balance' => 219.01,
'is_online' => false,
'created_at' => '2023-03-03 00:00:00',
],
[
'id' => 104,
'name' => 'Vitor',
'balance' => 44.28,
'is_online' => true,
'created_at' => '2023-04-04 00:00:00',
],
]);
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('balance', fn ($item) => Number::currency($item->balance, in: 'BRL', locale: 'pt-BR'))
->add('is_online', fn ($item) => $item->is_online ? '' : '')
->add('created_at', fn ($item) => Carbon::parse($item->created_at))
->add('created_at_formatted', fn ($item) => Carbon::parse($item->created_at)->format('d/m/Y'));
}
 
public function columns(): array
{
return [
Column::make('Index', 'id')->index(),
 
Column::make('ID', 'id'),
 
Column::add()
->title('Name')
->field('name')
->searchable()
->sortable(),
 
Column::add()
->title('Balance')
->field('balance')
->sortable(),
 
Column::add()
->title('Online')
->field('is_online'),
 
Column::add()
->title('Created At')
->field('created_at_formatted'),
];
}
 
public function filters(): array
{
return [
Filter::inputText('name'),
 
Filter::number('balance')
->thousands('.')
->decimal(',')
->placeholder('lowest', 'highest'),
 
Filter::boolean('is_online')
->label('', ''),
];
}
}
Code highlighting provided by Torchlight.dev