LoginSignup
0
1

PHPSandboxで、laravelのmiddlewareの練習①_権限テーブル作成、ダミーデータでログインまで

Last updated at Posted at 2024-04-15

内容

表題の通り。

なぜ記事にした?

・laravelのmiddlewareを使った実装の練習をしたかった
・環境を作るのを省くため、phpsandboxでやりたかった
・ダミーデータでログインする方法が割と書いてない

参考

https://qiita.com/shunpeister/items/5ff1d71aedaf86712371
こちらの記事を、phpsandboxで実践します

前提知識

phpsandboxのbash、sqliteの操作

本編、手順

以下、手順です

プロジェクトとrolesテーブルの作成

  1. テンプレートから、laravel10 breeze(blade)のプロジェクト?(notebook)を作成
  2. php artisan make:migration create_roles_tableを実行:マイグレーション作成。ユーザーマイグレーションファイルは既にあるのがいいところ
  3. migrationファイルの中身を変更(※下記)
  4. php artisan migrateを実行
  5. sqlite3 database.spliteを実行:テーブルの確認

結果
image.png

※3のマイグレーションファイルの中身

~create_roles_table.php
    public function up(): void
    {
        // Schema::create('roles', function (Blueprint $table) {
        //     $table->id();
        //     $table->timestamps();
        // });

        Schema::create('roles', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });

        // Inserting initial data
        DB::table('roles')->insert([
            ['name' => '管理者'],
            ['name' => 'リーダー'],
            ['name' => 'マネージャー'],
            ['name' => 'メンバー'],
        ]);
    }
~create_users_table.php
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
 +          $table->foreignId('role_id')->constrained('roles');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

ダミーデータの作成、ログイン

  1. userFactory.phprole_idを追加(※コードは下記)
  2. ./database/seeders/DatabaseSeeder.phpについて、コメントアウトを外す。
  3. php artisan db:seedを実行

ここまでやると、ダミーで作ったメールアドレスとパスワード=password123でログインできるようになる

※1.のコード

userFactory.php
    public function definition(): array
    {
+       $plainPassword = 'password123'; // 平文のパスワードをあらかじめ指定してしまう
+       $hashedPassword = bcrypt($plainPassword); // 平文のパスワードをハッシュ化
        return [
            'name' => fake()->name(),
            'email' => fake()->unique()->safeEmail(),
            'email_verified_at' => now(),
-           //'password' => static::$password ??= Hash::make('password'),
+           'password' => $hashedPassword, // パスワード
+           'role_id' => $this->faker->numberBetween(1, 4), // 1~4のランダムな整数
            'remember_token' => Str::random(10),
        ];
    }

※2. のコード

DatabaseSeeder.php
    public function run(): void
    {
        \App\Models\User::factory(10)->create(); //コメントアウトを外す
    }

結果確認
image.png
image.png

ダミーデータ作成でのトラブルった場合、下記をやってみると解消するかと思います
php artisan migrate:freshを実行
php artisan migrateを再度実行


次回②はロール権限毎にルーティング

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