LoginSignup
2
2

More than 3 years have passed since last update.

【Laravel】バーコードを表示する

Last updated at Posted at 2020-09-26

流れ

1、パッケージ選定
2、インストール
3、実装

パッケージ選定

Packagistで「barcode Laravel」で検索します。
スクリーンショット 2020-09-25 10.38.33.png
最上位に表示されている、ダウンロード数とスター数が一番多いmilon/barcodeを利用します。

ダウンロード数やスター数が多いパッケージを利用する理由は、
詰まった時に解決する術をどこかで見つけられる可能性が高いからです。

インストール

下記コマンドで実行。

composer require milon/barcode

こけました。。

Your requirements could not be resolved to an installable set of packages.
  Problem 1
     - milon/barcode 7.0.0 requires illuminate/support ^7.0
(抜粋)

illuminate/support ^7.0が必要と言われてますね。
バージョン指定しない場合は、最新のものをインストールしようとするので、
古めのLaravel5.8を使っていたのでダメだったみたいですね。

なのでバージョン指定をして再度インストールします。
Laravel5.8でしたがLaravel5.0 and 5.1Laravel6.*
間に記述がなかったので、ちょっと迷いました。

スクリーンショット 2020-09-25 11.07.07.png

試しにLaravel6.*扱いの下記コマンドで実行したところ、
無事インストールできました。

composer require milon/barcode:"6.0"

実装

バーコード表示のための準備で、
app.phpでプロバイダとエイリアスの登録を行います。

app.php

    'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        (省略)

        /*
         * Package Service Providers...
         */
      + Milon\Barcode\BarcodeServiceProvider::class,

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        App\Providers\CommonServiceProvider::class,

    ],

    /*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don't hinder performance.
    |
    */

    'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Arr' => Illuminate\Support\Arr::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        (省略)
        'Validator' => Illuminate\Support\Facades\Validator::class,
        'View' => Illuminate\Support\Facades\View::class,

      + 'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
      + 'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,

    ],

Webページにバーコードを表示するので、HTMLで生成。

$barcode = DNS1D::getBarcodeHTML(1234567A, "C128", 3, 70, 'black', true);

この変数をbladeファイルに渡して、表示したい場所にてechoで出力。
(1234567Aは架空の数字です。)

home.blade.php
<?php echo $barcode ?>

スクリーンショット 2020-09-26 10.20.19.png

メソッドのgetBarcodeHTMLは、

第一引数に、コード
第二引数に、バーコードのタイプ
第三引数に、高さ
第四引数に、幅
第五引数に、色
第六引数に、下記にコードを表示するかどうかの真偽値

をとる形ですね。

まとめ

少しつまるところもあったので、復習で記事にまとめました。おわり。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2