Workflow
Custom Field Image

This example demonstrates how to use a Custom Field to display an image.

*images randomly selected from thispersondoesnotexist.com

XLSX
Csv
Index
Avatar
Name
Balance
Created At
Index
Avatar
Name
Balance
Created At
1
Luan
R$ 241,86
01/01/2023
2
Daniel
R$ 166,51
02/02/2023
3
Claudio
R$ 219,01
03/03/2023
4
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\CustomFieldImageTable;
 
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Number;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Exportable;
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 CustomFieldImageTable extends PowerGridComponent
{
use WithExport;
 
public function setUp(): array
{
return [
Exportable::make('export')
->striped()
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV),
 
Header::make()
->showToggleColumns()
->showSearchInput(),
 
Footer::make()
->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('avatar', fn ($item) => '<img class="w-8 h-8 shrink-0 grow-0 rounded-full" src="' . asset("images/avatars/{$item->id}.jpeg") . '">')
->add('balance', fn ($item) => Number::currency($item->balance, in: 'BRL', locale: 'pt-BR'))
->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('Avatar', 'avatar'),
 
Column::add()
->title('Name')
->field('name')
->searchable()
->sortable(),
 
Column::add()
->title('Balance')
->field('balance')
->sortable(),
 
Column::add()
->title('Created At')
->field('created_at_formatted'),
];
}
}
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.