Custom Field Image
This example demonstrates how to use a Custom Field to display an image.
*images randomly selected from thispersondoesnotexist.com
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\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
use PowerComponents\LivewirePowerGrid\Traits\WithExport;
final class CustomFieldImageTable extends PowerGridComponent
{
public string $tableName = 'custom-field-image-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('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'),
];
}
}