ここでは管理者と教員のテストデータを登録します.このために,まずシーダを生成します.
vagrant@ubuntu2204 laravelAuth $ php artisan make:seeder AdminsTableSeeder ⏎ INFO Seeder [database/seeders/AdminsTableSeeder.php] created successfully. vagrant@ubuntu2204 laravelAuth $ php artisan make:seeder ProfessorsTableSeeder ⏎ INFO Seeder [database/seeders/ProfessorsTableSeeder.php] created successfully. vagrant@ubuntu2204 laravelAuth $
生成されたシーダにテストデータを設定します.
database/seeders/AdminsTableSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Admin;
class AdminsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('admins')->delete();
Admin::create([
'name' => 'Taro Admin',
'email' => 'taro@sample.com',
'login_id' => 'taro',
'password' => bcrypt('admin'),
'created_at' => '2023-11-05 11:01:01',
'updated_at' => '2023-11-05 11:01:01'
]);
Admin::create([
'name' => 'Jiro Admin',
'email' => 'jiro@sample.com',
'login_id' => 'jiro',
'password' => bcrypt('admin'),
'created_at' => '2023-11-05 11:02:01',
'updated_at' => '2023-11-05 11:02:01'
]);
}
}
database/seeders/ProfessorsTableSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Professor;
class ProfessorsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('professors')->delete();
Professor::create([
'name' => 'Tama',
'email' => 'tama@sample.com',
'login_id' => 'tama',
'password' => bcrypt('professor'),
'created_at' => '2023-11-05 11:11:01',
'updated_at' => '2023-11-05 11:11:01'
]);
Professor::create([
'name' => 'Pochi',
'email' => 'poch@sample.com',
'login_id' => 'pochi',
'password' => bcrypt('professor'),
'created_at' => '2023-11-05 11:12:01',
'updated_at' => '2023-11-05 11:12:01'
]);
}
}
DatabaseSeeder には登録したい順にシーダを呼び出します.
database/seeders/DatabaseSeeder.php
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
// \App\Models\User::factory(10)->create();
// \App\Models\User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$this->call(UsersTableSeeder::class);
$this->call(AdminsTableSeeder::class);
$this->call(ProfessorsTableSeeder::class);
}
}
データベースをロールバックしてからテーブルの作成,シードの投入を行います.このとき,マイグレーションの Batch 数が 2 になっていることから,ロールバックも 2 度実行しなければならないことに注意して下さい.
vagrant@ubuntu2204 laravelAuth $ php artisan migrate:status ⏎ Migration name ..................................... Batch / Status 2014_10_12_000000_create_users_table ...................... [1] Ran 2014_10_12_100000_create_password_reset_tokens_table ...... [1] Ran 2019_08_19_000000_create_failed_jobs_table ................ [1] Ran 2019_12_14_000001_create_personal_access_tokens_table ..... [1] Ran 2023_11_03_101930_add_login_id_to_users_table ............. [1] Ran 2023_11_05_111159_create_admins_table ..................... [2] Ran 2023_11_05_111213_create_professors_table ................. [2] Ran vagrant@ubuntu2204 laravelAuth $ php artisan migrate:rollback ⏎ INFO Rolling back migrations. 2023_11_05_111213_create_professors_table ............... 18ms DONE 2023_11_05_111159_create_admins_table .................... 5ms DONE vagrant@ubuntu2204 laravelAuth $ php artisan migrate:rollback ⏎ INFO Rolling back migrations. 2023_11_03_101930_add_login_id_to_users_table ............ 1ms DONE 2019_12_14_000001_create_personal_access_tokens_table .... 6ms DONE 2019_08_19_000000_create_failed_jobs_table ............... 6ms DONE 2014_10_12_100000_create_password_reset_tokens_table ..... 5ms DONE 2014_10_12_000000_create_users_table ..................... 6ms DONE vagrant@ubuntu2204 laravelAuth $ php artisan migrate:status ⏎ Migration name ..................................... Batch / Status 2014_10_12_000000_create_users_table ...................... Pending 2014_10_12_100000_create_password_reset_tokens_table ...... Pending 2019_08_19_000000_create_failed_jobs_table ................ Pending 2019_12_14_000001_create_personal_access_tokens_table ..... Pending 2023_11_03_101930_add_login_id_to_users_table ............. Pending 2023_11_05_111159_create_admins_table ..................... Pending 2023_11_05_111213_create_professors_table ................. Pending vagrant@ubuntu2204 laravelAuth $ php artisan migrate ⏎ INFO Running migrations. 2014_10_12_000000_create_users_table .................... 15ms DONE 2014_10_12_100000_create_password_reset_tokens_table ..... 4ms DONE 2019_08_19_000000_create_failed_jobs_table ............... 8ms DONE 2019_12_14_000001_create_personal_access_tokens_table ... 11ms DONE 2023_11_03_101930_add_login_id_to_users_table ........... 14ms DONE 2023_11_05_111159_create_admins_table ................... 11ms DONE 2023_11_05_111213_create_professors_table ............... 11ms DONE vagrant@ubuntu2204 laravelAuth $ php artisan db:seed ⏎ INFO Seeding database. Database\Seeders\UsersTableSeeder ......................... RUNNING Database\Seeders\UsersTableSeeder .................. 616.74 ms DONE Database\Seeders\AdminsTableSeeder ........................ RUNNING Database\Seeders\AdminsTableSeeder ................. 402.12 ms DONE Database\Seeders\ProfessorsTableSeeder .................... RUNNING Database\Seeders\ProfessorsTableSeeder ............. 395.72 ms DONE vagrant@ubuntu2204 laravelAuth $
作成されたテーブルのスキーマと投入されたデータを SQLite で確認します.
vagrant@ubuntu2204 laravelAuth $ sqlite3 database/database.sqlite ⏎ SQLite version 3.38.2 2022-03-26 13:51:10 Enter ".help" for usage hints. sqlite> .tables ⏎ admins password_reset_tokens users failed_jobs personal_access_tokens migrations professors sqlite> .schema users ⏎ CREATE TABLE IF NOT EXISTS "users" ( "id" integer primary key autoincrement not null, "name" varchar not null, "email" varchar not null, "email_verified_at" datetime, "password" varchar not null, "remember_token" varchar, "created_at" datetime, "updated_at" datetime, "login_id" varchar not null, "student_id" integer not null ); CREATE UNIQUE INDEX "users_email_unique" on "users" ("email"); CREATE UNIQUE INDEX "users_login_id_unique" on "users" ("login_id"); CREATE UNIQUE INDEX "users_student_id_unique" on "users" ("student_id"); sqlite> .schema admins ⏎ CREATE TABLE IF NOT EXISTS "admins" ( "id" integer primary key autoincrement not null, "name" varchar not null, "email" varchar not null, "login_id" varchar not null, "password" varchar not null, "created_at" datetime, "updated_at" datetime ); CREATE UNIQUE INDEX "admins_email_unique" on "admins" ("email"); CREATE UNIQUE INDEX "admins_login_id_unique" on "admins" ("login_id"); sqlite> .schema professors ⏎ CREATE TABLE IF NOT EXISTS "professors" ( "id" integer primary key autoincrement not null, "name" varchar not null, "email" varchar not null, "login_id" varchar not null, "password" varchar not null, "created_at" datetime, "updated_at" datetime ); CREATE UNIQUE INDEX "professors_email_unique" on "professors" ("email"); CREATE UNIQUE INDEX "professors_login_id_unique" on "professors" ("login_id"); sqlite> .headers on ⏎ sqlite> select * from users; ⏎ id|name|email|email_verified_at|password|remember_token|created_at|updated_at|login_id|student_id 1|A. Sample|a@sample.com|2023-11-03 00:05:00|$2y$12$w3k7weq4InIebL6nFKeE1OvSHk1be7pIwmPkN6jzMaajtto9bmSCi||2023-11-03 00:01:01|2023-11-03 00:01:01|user_a|6300997 2|B. Sample|b@sample.com|2023-11-03 00:06:00|$2y$12$CCjH5bVU6ahpsLsFD1FlsOGLG3rZjL9zHfJ5Lxqpctgszzy7Y4SxO||2023-11-03 00:02:01|2023-11-03 00:02:01|user_b|6300998 3|C. Sample|c@sample.com|2023-11-03 00:07:00|$2y$12$60mReZ.d2Q7GuUb1WUyun.M/axvFPnMwMs33A67OZfDfwBsyKX/b6||2023-11-03 00:03:01|2023-11-03 00:03:01|user_c|6300999 sqlite> select * from admins; ⏎ id|name|email|login_id|password|created_at|updated_at 1|Taro Admin|taro@sample.com|taro|$2y$12$8FqP8HoGvNSwdqx9t4fwlu3hUe119Excs3SKzirCgAcAnEaXPu9hm|2023-11-05 11:01:01|2023-11-05 11:01:01 2|Jiro Admin|jiro@sample.com|jiro|$2y$12$tCT3IpPMpIolCgAOmMUBVO7G23jMSy9vsKun07xRDwDb1rrJeZmV2|2023-11-05 11:02:01|2023-11-05 11:02:01 sqlite> select * from professors; ⏎ id|name|email|login_id|password|created_at|updated_at 1|Tama|tama@sample.com|tama|$2y$12$C6flS2jVgTlcOOs79jyi3.QIjVONtyJ.ymvzgUCwlLIoRW6McREoa|2023-11-05 11:11:01|2023-11-05 11:11:01 2|Pochi|poch@sample.com|pochi|$2y$12$4RWpTWRx9unf8sDKvQU9ZuGFFu4ETv/Sw6OXbIGRzZpOuxTV70BHa|2023-11-05 11:12:01|2023-11-05 11:12:01 sqlite>