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のsailでseeder作成/実行してデータを投入する

Posted at

環境

php 8.3.6
laravel Framework 11.4.0
composer 2.7.2

seeder作成

./vendor/bin/sail php artisan make:seeder MasterAiChatSeeder

/database/seeders/MasterAiChatSeeder.phpのファイルを修正

<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class MasterAiChatSeeder extends Seeder
{
    public function run(): void
    {
        $params = 
        [
            ['chat' => 'reactは勉強中です'],
            ['chat' => 'laravelもやっています'],
            ['chat' => 'seederやFacadesを学習中です']
        ];

        $now = Carbon::now();
        foreach ($params as $param) {
            $param['created_at'] = $now;
            $param['updated_at'] = $now;
            DB::table('master_ai_chat')->insert($param);
        }
    }
}

DatabaseSeederに作成したseederを追加する

/database/seeders/DatabaseSeeder.phpのファイルを修正

<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
         $this->call([
            MasterAiChatSeeder::class,
        ]);
    }
}

seederを実行する

./vendor/bin/sail artisan migrate --seed

まとめ

「php artisan db:seed --class=MasterAiChatSeeder」のやり方だとうまく挙動しなかったので、備忘録的に残しておきます

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?