Laravel + Nuxt.js でプログラム③(テーブル拡張)

laravel+Nuxt.js
この記事は約21分で読めます。
記事内に広告が含まれます。

前回までの状態でJWSを使ったログイン機能は動作しますが、
メール認証がないので、正しいメールアドレスが登録されているのかわかりません。
そこで、認証用のトークンを付加したEmailを送り、メールが届いた場合にだけ承認するようにします。

スポンサーリンク

Userテーブルを拡張し、トークンを保存できるようにします。

database/migrations/2020_05_04_120000_add_email_verify.php

[php]
<?php

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

class AddEmailVerify extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table(‘users’, function (Blueprint $table) {
$table->boolean(‘verify_email’)->comment(‘認証済:1、未認証:0’)->default(0);
$table->string(‘verify_token’)->comment(‘認証用及びリマインダートークン’)->nullable();
$table->timestamp(‘verify_date’)->comment(‘トークン発行日時’)->nullable();
$table->string(‘verify_email_address’)->comment(‘仮登録メールアドレス’)->nullable();
//
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(‘users’, function (Blueprint $table) {
$table->dropColumn(‘verify_email’);
$table->dropColumn(‘verify_token’);
$table->dropColumn(‘verify_date’);
$table->dropColumn(‘verify_email_address’);

//
});
}
}

[/php]

その他にユーザーテーブルに追加したい項目をMigrateします。
ここでは名前カナや誕生日、住所などを追加しています。適宜修正してください。
その他住所情報の都道府県は別テーブルとして作成し、アソシエーションを張る形にしています。

database/migrations/2020_05_04_121000_add_information_user_table.php

[php]
<?php

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

class AddInformationUserTable extends Migration
{
/**
* ユーザー情報拡張用
*
* @return void
*/
public function up()
{
// 都道府県テーブル登録
Schema::create(‘prefs’, function (Blueprint $table) {
$table->bigIncrements(‘id’);
$table->string(‘name’)->comment("都道府県名");
$table->integer(‘area_id’)->comment("エリアID");
$table->string(‘area_name’)->comment("エリア名");
$table->timestamps();
});

// ユーザーテーブル拡張
Schema::table(‘users’, function (Blueprint $table) {
$table->string(‘name_kana’,128)->nullable()->comment("名前(カナ)");
$table->date(‘birthday’)->nullable()->comment("生年月日");
$table->enum(‘gender’,[‘male’,’female’])->nullable()->comment("性別");
$table->string(‘zip_cd’,7)->nullable()->comment("郵便番号");
$table->BigInteger(‘pref_id’)->nullable()->unsigned()->comment("県");
$table->string(‘address1’,255)->nullable()->comment("市区町村");
$table->string(‘address2’,255)->nullable()->comment("それ以下");
$table->string(‘address3’,255)->nullable()->comment("建物等");
$table->string(‘phone_number’,11)->nullable()->comment("電話番号");
$table->text(‘memo’)->nullable()->comment("備考");
$table->softDeletes();

$table->foreign(‘pref_id’)->references(‘id’)->on(‘prefs’)->onUpdate(‘cascade’)->onDelete(‘cascade’);
//
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// 外部キー削除
Schema::table(‘users’, function (Blueprint $table) {
$table->dropForeign(‘users_pref_id_foreign’);
});
// テーブル削除
Schema::dropIfExists(‘prefs’);

// カラム削除
Schema::table(‘users’, function (Blueprint $table) {
$table->dropColumn(‘name_kana’);
$table->dropColumn(‘birthday’);
$table->dropColumn(‘gender’);
$table->dropColumn(‘zip_cd’);
$table->dropColumn(‘pref_id’);
$table->dropColumn(‘address1’);
$table->dropColumn(‘address2’);
$table->dropColumn(‘address3’);
$table->dropColumn(‘phone_number’);
$table->dropColumn(‘memo’);

//
});
}
}

[/php]

マイグレーションを作成したら実行します。
[bash]
% php artisan migrate
[/bash]

シード値の設定

シーダーで都道府県情報を作成します。
database/seeds/DatabaseSeeder.php
[php]
<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
* Seed the application’s database.
*
* @return void
*/
public function run()
{
$this->call([PrefsTableSeeder::class]);
}
}

[/php]

database/seeds/PrefsTableSeeder.php
[php]
<?php

use Illuminate\Database\Seeder;

class PrefsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
DB::table(‘prefs’)->insert([‘id’ => 1, ‘name’ => ‘北海道’, ‘area_id’ => 1,’area_name’ => ‘北海道’]);
DB::table(‘prefs’)->insert([‘id’ => 2, ‘name’ => ‘青森県’, ‘area_id’ => 2,’area_name’ => ‘東北’]);
DB::table(‘prefs’)->insert([‘id’ => 3, ‘name’ => ‘岩手県’, ‘area_id’ => 2,’area_name’ => ‘東北’]);
DB::table(‘prefs’)->insert([‘id’ => 4, ‘name’ => ‘宮城県’, ‘area_id’ => 2,’area_name’ => ‘東北’]);
DB::table(‘prefs’)->insert([‘id’ => 5, ‘name’ => ‘秋田県’, ‘area_id’ => 2,’area_name’ => ‘東北’]);
DB::table(‘prefs’)->insert([‘id’ => 6, ‘name’ => ‘山形県’, ‘area_id’ => 2,’area_name’ => ‘東北’]);
DB::table(‘prefs’)->insert([‘id’ => 7, ‘name’ => ‘福島県’, ‘area_id’ => 2,’area_name’ => ‘東北’]);
DB::table(‘prefs’)->insert([‘id’ => 8, ‘name’ => ‘茨城県’, ‘area_id’ => 3,’area_name’ => ‘関東’]);
DB::table(‘prefs’)->insert([‘id’ => 9, ‘name’ => ‘栃木県’, ‘area_id’ => 3,’area_name’ => ‘関東’]);
DB::table(‘prefs’)->insert([‘id’ => 10,’name’ => ‘群馬県’, ‘area_id’ => 3,’area_name’ => ‘関東’]);
DB::table(‘prefs’)->insert([‘id’ => 11,’name’ => ‘埼玉県’, ‘area_id’ => 3,’area_name’ => ‘関東’]);
DB::table(‘prefs’)->insert([‘id’ => 12,’name’ => ‘千葉県’, ‘area_id’ => 3,’area_name’ => ‘関東’]);
DB::table(‘prefs’)->insert([‘id’ => 13,’name’ => ‘東京都’, ‘area_id’ => 3,’area_name’ => ‘関東’]);
DB::table(‘prefs’)->insert([‘id’ => 14,’name’ => ‘神奈川県’,’area_id’ => 3,’area_name’ => ‘関東’]);
DB::table(‘prefs’)->insert([‘id’ => 15,’name’ => ‘新潟県’, ‘area_id’ => 4,’area_name’ => ‘中部’]);
DB::table(‘prefs’)->insert([‘id’ => 16,’name’ => ‘富山県’, ‘area_id’ => 4,’area_name’ => ‘中部’]);
DB::table(‘prefs’)->insert([‘id’ => 17,’name’ => ‘石川県’, ‘area_id’ => 4,’area_name’ => ‘中部’]);
DB::table(‘prefs’)->insert([‘id’ => 18,’name’ => ‘福井県’, ‘area_id’ => 4,’area_name’ => ‘中部’]);
DB::table(‘prefs’)->insert([‘id’ => 19,’name’ => ‘山梨県’, ‘area_id’ => 1,’area_name’ => ‘中部’]);
DB::table(‘prefs’)->insert([‘id’ => 20,’name’ => ‘長野県’, ‘area_id’ => 4,’area_name’ => ‘中部’]);
DB::table(‘prefs’)->insert([‘id’ => 21,’name’ => ‘岐阜県’, ‘area_id’ => 4,’area_name’ => ‘中部’]);
DB::table(‘prefs’)->insert([‘id’ => 22,’name’ => ‘静岡県’, ‘area_id’ => 4,’area_name’ => ‘中部’]);
DB::table(‘prefs’)->insert([‘id’ => 23,’name’ => ‘愛知県’, ‘area_id’ => 4,’area_name’ => ‘中部’]);
DB::table(‘prefs’)->insert([‘id’ => 24,’name’ => ‘三重県’, ‘area_id’ => 5,’area_name’ => ‘近畿’]);
DB::table(‘prefs’)->insert([‘id’ => 25,’name’ => ‘滋賀県’, ‘area_id’ => 5,’area_name’ => ‘近畿’]);
DB::table(‘prefs’)->insert([‘id’ => 26,’name’ => ‘京都府’, ‘area_id’ => 5,’area_name’ => ‘近畿’]);
DB::table(‘prefs’)->insert([‘id’ => 27,’name’ => ‘大阪府’, ‘area_id’ => 5,’area_name’ => ‘近畿’]);
DB::table(‘prefs’)->insert([‘id’ => 28,’name’ => ‘兵庫県’, ‘area_id’ => 5,’area_name’ => ‘近畿’]);
DB::table(‘prefs’)->insert([‘id’ => 29,’name’ => ‘奈良県’, ‘area_id’ => 5,’area_name’ => ‘近畿’]);
DB::table(‘prefs’)->insert([‘id’ => 30,’name’ => ‘和歌山県’,’area_id’ => 5,’area_name’ => ‘近畿’]);
DB::table(‘prefs’)->insert([‘id’ => 31,’name’ => ‘鳥取県’, ‘area_id’ => 6,’area_name’ => ‘中国’]);
DB::table(‘prefs’)->insert([‘id’ => 32,’name’ => ‘島根県’, ‘area_id’ => 6,’area_name’ => ‘中国’]);
DB::table(‘prefs’)->insert([‘id’ => 33,’name’ => ‘岡山県’, ‘area_id’ => 6,’area_name’ => ‘中国’]);
DB::table(‘prefs’)->insert([‘id’ => 34,’name’ => ‘広島県’, ‘area_id’ => 6,’area_name’ => ‘中国’]);
DB::table(‘prefs’)->insert([‘id’ => 35,’name’ => ‘山口県’, ‘area_id’ => 6,’area_name’ => ‘中国’]);
DB::table(‘prefs’)->insert([‘id’ => 36,’name’ => ‘徳島県’, ‘area_id’ => 7,’area_name’ => ‘四国’]);
DB::table(‘prefs’)->insert([‘id’ => 37,’name’ => ‘香川県’, ‘area_id’ => 7,’area_name’ => ‘四国’]);
DB::table(‘prefs’)->insert([‘id’ => 38,’name’ => ‘愛媛県’, ‘area_id’ => 7,’area_name’ => ‘四国’]);
DB::table(‘prefs’)->insert([‘id’ => 39,’name’ => ‘高知県’, ‘area_id’ => 7,’area_name’ => ‘四国’]);
DB::table(‘prefs’)->insert([‘id’ => 40,’name’ => ‘福岡県’, ‘area_id’ => 8,’area_name’ => ‘九州’]);
DB::table(‘prefs’)->insert([‘id’ => 41,’name’ => ‘佐賀県’, ‘area_id’ => 8,’area_name’ => ‘九州’]);
DB::table(‘prefs’)->insert([‘id’ => 42,’name’ => ‘長崎県’, ‘area_id’ => 8,’area_name’ => ‘九州’]);
DB::table(‘prefs’)->insert([‘id’ => 43,’name’ => ‘熊本県’, ‘area_id’ => 8,’area_name’ => ‘九州’]);
DB::table(‘prefs’)->insert([‘id’ => 44,’name’ => ‘大分県’, ‘area_id’ => 8,’area_name’ => ‘九州’]);
DB::table(‘prefs’)->insert([‘id’ => 45,’name’ => ‘宮崎県’, ‘area_id’ => 8,’area_name’ => ‘九州’]);
DB::table(‘prefs’)->insert([‘id’ => 46,’name’ => ‘鹿児島県’,’area_id’ => 8,’area_name’ => ‘九州’]);
DB::table(‘prefs’)->insert([‘id’ => 47,’name’ => ‘沖縄県’, ‘area_id’ => 9,’area_name’ => ‘沖縄’]
);
}
}

[/php]

シーダを実行するためにパッケージを導入します。

[bash]
% composer dump-autoload
[/bash]

[bash]
% php artisan db:seed
[/bash]

Userモデルの編集

app/Models/User.php
[php]
<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
use Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
‘name’,
‘email’,
‘password’,
‘name_kana’,
‘birthday’,
‘gender’,
‘zip_cd’,
‘pref_id’,
‘address1’,
‘address2’,
‘address3’,
‘phone_number’,
‘memo’
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
‘password’,
‘remember_token’,
‘verify_email’,
‘verify_token’,
‘verify_date’,
‘verify_email_address’,
‘deleted_at’
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
‘email_verified_at’ => ‘datetime’,
];

// JWT用
public function getJWTIdentifier()
{
return $this->getKey();
}

public function getJWTCustomClaims()
{
return [];
}
}
[/php]

コメント

タイトルとURLをコピーしました