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-09-08 16:58:54
|
|
2
|
Albacore Tuna Melt
|
No
|
2024-08-23 22:44:44
|
|
3
|
Π±ΠΎΡΡ
|
Yes
|
2024-08-17 18:00:50
|
|
4
|
Bacalhau com natas
|
Yes
|
2024-08-26 09:45:45
|
|
5
|
Baba Ghanoush
|
Yes
|
2024-08-11 19:09:34
|
|
6
|
Bacon Cheeseburger
|
Yes
|
2024-08-16 01:20:24
|
|
7
|
Baked potato
|
Yes
|
2024-08-26 03:02:42
|
|
8
|
Baklava
|
Yes
|
2024-10-01 21:08:46
|
|
9
|
Bangers and mash
|
No
|
2024-08-15 06:23:48
|
|
10
|
Black Pudding
|
Yes
|
2024-08-14 16:49:20
|
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'), ]; }}
Code highlighting provided by Torchlight.dev