In this tutorial you will learn about the Laravel 8 Barcode Generator Example Tutorial and its application with practical example.
In this tutorial, we will learn how to generate barcode in laravel 8 application. I’ll guide you through step by step for generation of barcode in a laravel 8 application. We will be using ‘milon/barcode’ package for generating barcode in laravel 8. In this tutorial, we will guide you step by step to generate bar code in your laravel 8.
Contents:-
- Install Laravel 8
- Setup Database Credentials
- Install milon/barcode Package
- Configure Barcode Generator Package
- Define Route
- Creating BarCode Controller
- Create Blade View
- Start Development Server
Install Laravel 8 Application
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Database Configuration
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 7 8 9 10 11 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db name DB_USERNAME=db user name DB_PASSWORD=db password |
Installing Barcode Generator Package
We will be using “milon/barcode” Package” for generating barcode in laravel 8 application. In this step we will be installing “milon/barcode” via following composer command, lets open your terminal and switch to the project directory and enter the following command –
1 |
composer require milon/barcode |
Configure Barcode Generator Package
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
'providers' => [ .... Milon\Barcode\BarcodeServiceProvider::class, ], 'aliases' => [ .... 'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class, 'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class, ] |
Create Routes
After this, we need to add following two routes in “routes/web.php” to display barcode. Lets open “routes/web.php” file and add following route.
routes/web.php
1 2 |
use App\Http\Controllers\BarcodeController; Route::get('/barcode', [BarcodeController::class, 'index'])->name('barcode.index'); |
Creating BarCode Controller
Now, lets create a controller for barcode generation. Create a controller named BarcodeController using command given below –
1 |
php artisan make:controller BarcodeController |
Once the above command executed, it will create a controller file BarcodeController.php in app/Http/Controllers/ directory. Open the BarcodeController.php file and put the following code in it.
app/Http/Controllers/BarcodeController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class BarcodeController extends Controller { public function index() { return view('barcode'); } } |
Create Blade View
In this step, we will create view/blade file to display barcode. Lets create a “barcode.blade.php” file in “resources/views/” directory and put the following code in it.
resources/views/barcode.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!DOCTYPE html> <html> <head> <title>Laravel 8 Barcode Generator</title> </head> <body> <div class="container text-center"> <div class="row"> <div class="col-md-8 offset-md-2"> <h1 class="mb-5">Laravel 8 Barcode Generator</h1> <div>{!! DNS1D::getBarcodeHTML('4445645656', 'C39') !!}</div></br> <div>{!! DNS1D::getBarcodeHTML('4445645656', 'POSTNET') !!}</div></br> <div>{!! DNS1D::getBarcodeHTML('4445645656', 'PHARMA') !!}</div></br> <div>{!! DNS2D::getBarcodeHTML('4445645656', 'QRCODE') !!}</div></br> </div> </div> </div> </body> </html> |
Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
http://localhost:8000/barcode