Laravel 8 barcode generator example. In this tutorial, i would like to show you how to generate or create barcode in laravel 8 using milon/barcode package.
Using this milon/barcode package, you can generate simple bar code, image bar code, text bar code in laravel 8 app.
In this tutorial, i will use milon/barcode package to generate simple, text, numeric and image barcode in laravel 8 app.
Laravel 8 BarCode Generator Tutorial
Use the below given simple steps to generate different types of bar code in laravel 8:
- Step 1 – Install Laravel 8 Application
- Step 2 – Database Configuration
- Step 3 – Installing Barcode Generator Package
- Step 4 – Configure Barcode Generator Package
- Step 5 – Create Routes
- Step 6 – Creating Bar Code Controller
- Step 7 – Create Blade View
- Step 8 – Run Development Server
- Step 9 – Run This App on Browser
Step 1 – Install Laravel 8 Application
In step 1, install laravel 8 new setup. So, open terminal and type the following command to install new laravel 8 app into your machine:
composer create-project --prefer-dist laravel/laravel LaravelPDFGenerate
Step 2 – Database Configuration
In step 2, configure database with your downloded/installed laravel 8 app. So, you need to find .env file and setup database details as following:
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
Step 3 – Installing Barcode Generator Package
In step 3, install milon/barcode package in laravel 8 app via following command:
composer require milon/barcode
Step 4 – Configure Barcode Generator Package
In step 4, configure the milon/barcode package in laravel 8 app. So, Open the providers/config/app.php file and register the provider and aliases for milon/barcode.
'providers' => [ .... Milon\Barcode\BarcodeServiceProvider::class, ], 'aliases' => [ .... 'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class, 'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class, ]
Step 5 – Create Routes
In step 5, add the bar code generation routes in web.php file, which is located inside routes directory:
use App\Http\Controllers\BarCodeController; Route::get(bar-code', [BarCodeController::class, 'index'])
Step 6 – Creating BarCode Controller
In this step, create generate barcode controller file by using the following command:
php artisan make:controller BarCodeController
Then navigate to app/http/controllers and open BarCodeController.php file. And add the simple barcode generation code into it:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class BarCodeController extends Controller { public function index() { return view('barcode-generator'); } }
Step 7 – Create Blade View
In step 7, create barcode-generator blade view file inside resources/views directory. And then add the following code into it:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Laravel 8 Barcode Generator Tutorial</title> <link rel="stylesheet" href="{{asset('css/app.css')}}"> </head> <body> <h2>Laravel 8 Barcode Generator Examples</h2> <div class="container text-center mt-2"> <h2 class="mb-2 mb-2">One-Dimensional (1D) Barcode Types</h2> <div>{!!DNS1D::getBarcodeHTML(8889899, 'C39')!!}</div></br> <div>{!!DNS1D::getBarcodeHTML(5436564, 'S25')!!}</div></br> <div>{!!DNS1D::getBarcodeHTML(77656765, 'I25')!!}</div></br> <div>{!!DNS1D::getBarcodeHTML(6435636, 'MSI+')!!}</div></br> <div>{!!DNS1D::getBarcodeHTML(25547, 'POSTNET')!!}</div></br> </div> </body> </html>
Step 8 – Run Development Server
In step 8, open command prompt and type the below given command to run development server:
php artisan serve
Step 8 – Run this App On Browser
Now, open your browser and hit the following url on it:
http://127.0.0.1:8000/bar-code