0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravelでゲストユーザー用Seederを作成し .env でパスワードを管理する方法

Last updated at Posted at 2025-07-31

目的

Laravelでゲストログイン機能を実装する際、「ゲストユーザーの初期データを用意したい」「でもパスワードはコードにハードコーディングしたくない」というケースに対応する方法をまとめます。

やること

▪️ ゲストユーザーを作成

▪️ .env でパスワードを管理

▪️ ゲスト用のブランド・弁当データもSeederで用意

手順

順番 内容
1 .env にパスワードを定義
2 config/app.php に設定を追加
3 ゲストユーザーSeederの作成
4 ゲスト専用のデモデータSeederの作成
5 DatabaseSeeder.php に追記
6 Seederの実行

✅ 1:.env にパスワードを定義

.env に以下を追記:

.env
GUEST_EMAIL=xxx@xxx.com
GUEST_PASSWORD=xxxxxxxx

✅ 2:config/app.php に設定を追加

config/app.php
return [

    // ...

    'guest_email' => env('GUEST_EMAIL'),
    'guest_password' => env('GUEST_PASSWORD'),
];

✅ 3:ゲストユーザーSeederの作成

database/seeders/GuestUserSeeder.php
use Illuminate\Database\Seeder;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class GuestUserSeeder extends Seeder
{
    public function run(): void
    {
        User::firstOrCreate(
            ['email' => config('app.guest_email')],
            [
                'name' => 'ゲスト',
                'password' => Hash::make(config('app.guest_password')),
            ]
        );
    }
}

✅ 4:ゲスト専用のデモデータSeederの作成

database/seeders/GuestBentoSeeder.php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use App\Models\User;

class GuestBentoSeeder extends Seeder
{
    public function run(): void
    {
        $guest = User::where('email', config('app.guest_email'))->first();

        $brands = ['ほっともっと', 'オリジン弁当', '玉子屋', '日本亭'];

        foreach($brands as $brand) {
            DB::table('bento_brands')->insert([
                ['user_id' => 1, 'name' => $brand], // 管理者用
                ['user_id' => $guest->id, 'name' => $brand], // ゲスト用
            ]);
        }

        $bentos = [
            'ほっともっと' => ['野菜炒め弁当', '唐揚げ弁当', 'チキン南蛮弁当'],
            'オリジン弁当' => ['タルタルのり弁', 'カツ丼', '牛焼肉弁当'],
            '玉子屋' => ['日替わり弁当'],
            '日本亭' => ['満腹生姜焼き弁当', 'ロースカツ丼', '酢豚弁当'],
        ];

        foreach($bentos as $brandName => $names) {
            // 管理者のブランドIDを取得
            $brand_admin = DB::table('bento_brands')
                ->where('name', $brandName)
                ->where('user_id', 1)
                ->first();
    
            // ゲストのブランドIDを取得
            $brand_guest = DB::table('bento_brands')
                ->where('name', $brandName)
                ->where('user_id', $guest->id)
                ->first();

            foreach($names as $name) {
                // 管理者用
                DB::table('bento_names')->insert([
                    'user_id' => 1,
                    'bento_brand_id' => $brand_admin->id,
                    'name' => $name,
                ]);
    
                // ゲスト用
                DB::table('bento_names')->insert([
                    'user_id' => $guest->id,
                    'bento_brand_id' => $brand_guest->id,
                    'name' => $name,
                ]);
            }
        }
    }
}

✅ 5:DatabaseSeeder.php に追記

database/seeders/DatabaseSeeder.php
public function run(): void
{
    $this->call([
        GuestUserSeeder::class,
        GuestBentoSeeder::class,
    ]);
}

✅ 6:Seederの実行

php artisan db:seed
# または初期化込みで
php artisan migrate:fresh --seed
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?