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
|
|
No
|
2024-04-03 23:22:50
|
2
|
Albacore Tuna Melt
|
|
Yes
|
2024-05-18 19:37:38
|
3
|
борщ
|
|
Yes
|
2024-04-30 09:42:56
|
4
|
Bacalhau com natas
|
|
No
|
2024-04-21 04:00:16
|
5
|
Baba Ghanoush
|
|
No
|
2024-05-01 10:58:39
|
6
|
Bacon Cheeseburger
|
|
No
|
2024-05-17 18:53:32
|
7
|
Baked potato
|
|
No
|
2024-04-13 05:50:41
|
8
|
Baklava
|
|
Yes
|
2024-04-21 03:39:35
|
9
|
Bangers and mash
|
|
No
|
2024-04-03 21:20:32
|
10
|
Black Pudding
|
|
Yes
|
2024-05-18 17:21:27
|
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\Footer;use PowerComponents\LivewirePowerGrid\Header;use PowerComponents\LivewirePowerGrid\PowerGrid;use PowerComponents\LivewirePowerGrid\PowerGridComponent;use PowerComponents\LivewirePowerGrid\PowerGridFields; class CustomFieldBarcodeTable extends PowerGridComponent{ public function setUp(): array { return [ Header::make() ->showSearchInput(), Footer::make() ->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
Here you can find all relevant packages installed on this demo.
Name
|
Version
|
Description
|
---|---|---|
laravel/framework
|
v11
|
The Laravel Framework.
|
livewire/livewire
|
v3.5.0
|
A front-end framework for Laravel.
|
openspout/openspout
|
v4.24.1
|
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
|
picqer/php-barcode-generator
|
v2.4.0
|
An easy to use, non-bloated, barcode generator in PHP. Creates SVG, PNG, JPG and HTML images from the most used 1D barcode standards.
|
power-components/livewire-powergrid
|
5.x-dev
|
PowerGrid generates Advanced Datatables using Laravel Livewire.
|