Custom Field Barcode
This example uses Custom Fields in combination with the package PHP Barcode Generator to display barcodes in each table row.
ID
|
Name
|
Barcode
|
In Stock
|
Created At
|
---|---|---|---|---|
1
|
Arkansas Possum Pie
|
Yes
|
2024-11-03 17:31:54
|
|
2
|
Albacore Tuna Melt
|
No
|
2024-12-01 06:52:53
|
|
3
|
Π±ΠΎΡΡ
|
No
|
2024-12-02 18:47:46
|
|
4
|
Bacalhau com natas
|
No
|
2024-11-22 20:23:08
|
|
5
|
Baba Ghanoush
|
No
|
2024-11-02 02:05:58
|
|
6
|
Bacon Cheeseburger
|
Yes
|
2024-11-04 21:40:53
|
|
7
|
Baked potato
|
Yes
|
2024-10-28 08:26:06
|
|
8
|
Baklava
|
Yes
|
2024-12-06 18:11:56
|
|
9
|
Bangers and mash
|
Yes
|
2024-12-11 05:05:54
|
|
10
|
Black Pudding
|
Yes
|
2024-12-18 17:33:29
|
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\CustomFieldBarcodeTable;
use App\Models\Dish;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
class CustomFieldBarcodeTable extends PowerGridComponent
{
public string $tableName = 'custom-field-barcode-table';
public function setUp(): array
{
return [
PowerGrid::header()
->showSearchInput(),
PowerGrid::footer()
->showPerPage()
->showRecordCount(),
];
}
public function datasource(): ?Builder
{
return Dish::query();
}
public function fields(): PowerGridFields
{
$barcodeGenerator = new \Picqer\Barcode\BarcodeGeneratorPNG;
return PowerGrid::fields()
->add('id')
->add('name')
->add('in_stock', fn ($dish) => $dish->in_stock ? 'Yes' : 'No')
->add('created_at_formatted', fn ($dish) => Carbon::parse($dish->created_at))
->add('barcode', function (Dish $dish) use ($barcodeGenerator) {
return sprintf(
'<img src="data:image/png;base64,%s">',
base64_encode($barcodeGenerator->getBarcode($dish->id, $barcodeGenerator::TYPE_CODE_128))
);
});
}
public function columns(): array
{
return [
Column::make('ID', 'id')
->searchable()
->sortable(),
Column::make('Name', 'name')
->searchable()
->sortable(),
Column::make('Barcode', 'barcode'),
Column::make('In Stock', 'in_stock')
->searchable(),
Column::make('Created At', 'created_at_formatted'),
];
}
}