article

Sunday, March 19, 2023

Laravel Generate Barcode

Laravel Generate Barcode

Download Laravel App

composer create-project --prefer-dist laravel/laravel my-app
C:\xampp\htdocs\laravel>composer create-project --prefer-dist laravel/laravel my-app

Connecting our Database

open .env file root directory.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=root
DB_PASSWORD=

Create Controller ProductController
php artisan make:controller ProductController
C:\xampp\htdocs\laravel\my-app>php artisan make:controller ProductController
app\Http\Controllers\ProductController.php

//app\Http\Controllers\ProductController.php
<?php

namespace App\Http\Controllers;

use App\Models\product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index(){
        $products = product::all();
        return view('index',compact('products'));
    }

    public function create(){
        return view('create');
    }

    public function store(Request $request){
        $number = mt_rand(1000000000,9999999999);
        
        if ($this->productCodeExists($number)) {
            $number = mt_rand(1000000000,999999999);
        }
        
        $request['product_code'] = $number;
        Product::create($request->all());

        return redirect('/');
    }

    public function productCodeExists($number){
        return product::whereProductCode($number)->exists();
    }
}
Database Migration
php artisan make:model Product -m
C:\xampp\htdocs\laravel\my-app>php artisan make:model Product -m
database/migrations/create_products_table.php
//database/migrations/create_products_table.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('price');
            $table->string('product_code');
            $table->text('description');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('products');
    }
};
run this migration
C:\xampp\htdocs\laravel\my-app>php artisan migrate

Model Product

app/models/Product.php
//app/models/Product.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'price',
        'product_code',
        'description'
    ];
}
Install Barcode
https://github.com/milon/barcode

composer require milon/barcode
C:\xampp\htdocs\laravel\my-app>composer require milon/barcode

Bootstrap 5
https://getbootstrap.com/docs/5.0/getting-started/introduction/
https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css

resources/views/index.blade.php
//resources/views/index.blade.php
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel Generate Barcode</title>       
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" />
</head>
<body>
<div class="container"><h1 class="text-primary pt-4 text-center mb-4">Laravel Generate Barcode</h1>
    <div class="row justify-content-md-center">
        <h1 class="pt-4 text-left mb-4"><b>List of Products</b></h1>
        <hr>
        <div class="pb-2">
          <a href="/create" class="btn btn-success">New Post</a>
        </div>
        <table class="table table-hover">
            <thead>
                <tr>
                    <th scope="col">ID</th>
                    <th scope="col">Title</th>
                    <th scope="col">Price</th>
                    <th scope="col">Barcode</th>
                    <th scope="col">Description</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($products as $product)
                    <tr>
                        <th>{{ $product->id }}</th>
                        <td>{{ $product->title }}</td>
                        <td>{{ $product->price }}</td>
                        <td>{!! DNS1D::getBarcodeHTML("$product->product_code",'UPCA',2,50) !!}
                         p - {{ $product->product_code }}
                        </td>
                        <td>{{ $product->description }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
resources/views/create.blade.php
//resources/views/create.blade.php
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel Generate Barcode</title>       
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" />
</head>
<body>
    <div class="container-fluid  ">
      <div class="row justify-content-md-center">
        <div class="col-md-9">
            <div class="card px-5 mt-3  shadow">
               <h1 class="text-primary pt-4 text-center mb-4">Laravel Generate Barcode</h1>
                <form action="/post" method="post">
                 @csrf
                    <label for="">Title:</label>
                    <input type="text" class="form-control mb-3" name="title" required>
                    <label for="">Price:</label>
                    <input type="number" class="form-control mb-3" name="price" required>
                    <label for="">Description:</label>
                    <textarea name="description" class="form-control mb-3" cols="30" rows="5" required></textarea>
                 <button type="submit" class="btn btn-success col-md-3">Submit</button>
            </form>
        </div>
        </div>
      </div>
    </div>
</body>
</html>
Routes
routes/web.php
//routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;

//Route::get('/', function () {
//    return view('welcome');
//});
Route::get('/',[ProductController::class,'index']);
Route::get('/create',[ProductController::class,'create']);
Route::post('/post',[ProductController::class,'store']);
Run C:\xampp\htdocs\laravel\my-app>php artisan serve
Starting Laravel development server: http://127.0.0.1:8000

Related Post