6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Laravel+Docker】Laradockでuuidを使う

6
Last updated at Posted at 2019-03-13

Laradock環境でuuidを使いたい!!

個人的にキリンさんよりもゾウさんよりもクジラさん(docker)が好きなので、なんでもかんでもdockerで環境構築してみてるんですが、案外ローカル環境とは違うところで地味に詰まったりする事が多いと感じます。

今回もdockerでlaravelの開発環境を使いたくLaradockを使っていた所、tableでuuidを使用しようとした際に軽く詰まったので記録。

そもそもincrementのidではなく、uuidを使おうとしたのはセキュリティ上incrementだとユーザー数が把握されやすいという事があり、それを防ぐ為に導入したいと思った次第です。

環境

Laravel 5.6以上
Mysql 5.6以上

話さない事

Laradockやdockerの基本的な部分についてはここでは触れません。導入手順についてもLaradockでLaravel環境を整えた前提でお話させてもらいます。詳しく知りたい方はこちらなどをご参照ください。

導入手順

手順は下記の通りでシンプル。

  1. migrationファイルを変更
  2. Userモデルを変更
  3. 各種コンテナを起動
  4. 立ち上げたworkspaceコンテナ内に入る
  5. moontoast/mathをインストールする
    注意:bcmathがインストールされていなければ、bcmathをインストールする

1. Migrationファイルを変更

まずusersテーブルのmigrationファイルにてdefaultで設定されているincrementからuuidに変更

database/migrations/create_users_table.php

- $table->increment('id');
+ $table->uuid('id')->primary()

2. Userモデルを変更

次にappディレクトリ下のUserモデルを変更

app/User.php
+ public $incrementing = false;
+ public static function boot()
+ {
+      parent::boot();
+      self::creating(function($model){
+      $model->id=(string)\Illuminate\Support\Str::uuid();
+      });
+ }

上記のuuidメソッドでは完全にランダムなuuidが生成されます。他にもorderedUuidメソッドにてインデックス可能なuuidの生成も可能です。

3.各種dockerコンテナを起動

ファイルの編集が終わったらlaradockディレクトリに移ってdockerコンテナ達を呼び起こしましょう

/laradock
$ docker-compose up -d nginx mysql
Starting laradock_mysql_1            ... done
Starting laradock_docker-in-docker_1 ... done
Starting laradock_workspace_1        ... done
Starting laradock_php-fpm_1          ... done
Starting laradock_nginx_1            ... done

4.workspaceコンテナに入る

/laradock
$ docker-compose exec workspace bash
/var/www# 

5. moontoast/mathをインストールする

uuidを発行する為に必要なmoontoast/mathというパッケージをインストールしてあげます。

workspace内

/var/www# composer require moontoast/math

これで準備は万端。

確認

ちゃんと動作するか確認してみましょう。

workspace内
/var/www# php artisan migrate:fresh

でテーブルを一度更新し、DBとのやり取りを対話的に行えるtinkerを立ち上げ、

workspace内
/var/www# php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> 

Userモデルを下記の様に「factory(App\User::class)->create();」で生成すると、

workspace内
/var/www# php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> factory(App\User::class)->create();
=> App\User {#2939
     name: "Wallace Krajcik",
     email: "eichmann.loraine@example.net",
     email_verified_at: "2019-03-11 14:46:34",
     id: "8d2ea9f4-fb8e-4463-97d9-5965dc12486c",
     updated_at: "2019-03-11 14:46:34",
     created_at: "2019-03-11 14:46:34",
   }

と表示され、idにしっかりとuuidが指定されている事が確認出来ました。

注意点

実はMoontoast/mathを使うにあたってはPHPにbcmathというライブラリもインストールされている必要があります。Moontoast/mathとbcmathのどちらかがインストールされていない場合、

workspace内
/var/www# php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> factory(App\User::class)->create();
Ramsey/Uuid/Exception/UnsatisfiedDependencyException with message 'Cannot call Ramsey/Uuid/Converter/Number/DegradedNumberConverter::toHex without support for large integers, since integer is an unsigned 128-bit integer; Moontoast/Math/BigNumber is required. '

と怒られてしまいます。bcmathがインストールされているか不明な時は、

workspace内
/var/www# php -m | grep bcmath

で念の為、bcmathがインストールされているか確認しましょう。

workspace内
/var/www# php -m | grep bcmath
bcmath
/var/www#

と表示されたら無事bcmathがインストールされていますが、何も表示されない様であれば、

workspace内
/var/www# docker-php-ext-install bcmath

でインストールしてあげましょう。

またlaradockでuuidを使う際はworkspaceコンテナだけでなく、php-fpmコンテナにもbcmathがインストールされている必要があるので、まだうまく行かない人は

php-fpm内
/var/www# docker-php-ext-install bcmath

でphp-fpmコンテナにもbcmathをインストールしてあげましょう。

参考にしたサイト

https://blog.e2info.co.jp/2018/03/19/laravel5-6_uuid-methods/
https://xzxzyzyz.com/2018/02/laravel-primary-uuid/
https://blog.capilano-fw.com/?p=277

6
1
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
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?