Custom Field Blade Component
This example demonstrates how to use a Custom Field to display a Blade Component in a Table.
The Rate Blade Component source is available in this GitHub Repository.
ID
|
Name
|
Rating
|
Created At
|
---|---|---|---|
1
|
Arkansas Possum Pie
|
β
β
β
β
β
|
03/11/2024
|
2
|
Albacore Tuna Melt
|
β
β
β
ββ
|
01/12/2024
|
3
|
Π±ΠΎΡΡ
|
β
β
βββ
|
02/12/2024
|
4
|
Bacalhau com natas
|
β
β
β
ββ
|
22/11/2024
|
5
|
Baba Ghanoush
|
β
β
β
β
β
|
02/11/2024
|
6
|
Bacon Cheeseburger
|
β
β
β
β
β
|
04/11/2024
|
7
|
Baked potato
|
β
β
β
ββ
|
28/10/2024
|
8
|
Baklava
|
β
β
β
ββ
|
06/12/2024
|
9
|
Bangers and mash
|
β
ββββ
|
11/12/2024
|
10
|
Black Pudding
|
β
β
β
β
β
|
18/12/2024
|
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\CustomFieldBladeComponentTable;
use App\Models\Dish;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Blade;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
final class CustomFieldBladeComponentTable extends PowerGridComponent
{
public string $tableName = 'custom-field-blade-table';
public function setUp(): array
{
return [
PowerGrid::footer()
->showPerPage()
->showRecordCount(),
];
}
public function datasource(): ?Builder
{
return Dish::query();
}
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('rating_stars', fn ($dish) => Blade::render('<x-rate rate="' . $dish->rating . '"/>'))
->add('created_at_formatted', fn ($dish) => Carbon::parse($dish->created_at)->format('d/m/Y'));
}
public function columns(): array
{
return [
Column::make('ID', 'id'),
Column::make('Name', 'name'),
Column::make('Rating', 'rating_stars'),
Column::make('Created At', 'created_at_formatted'),
];
}
}