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?

SeederでDBにテストデータを用意

Last updated at Posted at 2024-10-23

前提

最初にシーダ―(seeder)でテストデータを用意しておくとよい。

  • ダミーデータがサクッと準備できるのでスムーズに開発に入れる
  • データをすぐ元通りにできる

以下のようなcustomersテーブルを例にする
image.png
※手順内のモデル名、テーブル名は適宜書き替えること

マイグレーションファイル

【補足1 customersのマイグレーションファイル】
database\migrations\2024_10_17_015108_create_customers_table.php
<?php
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    return new class extends Migration
        {
            /**
             * Run the migrations.
             *
             * @return void
             */
            public function up()
            {
                Schema::create("customers", function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('phone')->nullable();
                $table->timestamps();               
                });
            }

            /**
             * Reverse the migrations.
             *
             * @return void
             */
            public function down()
            {
                Schema::dropIfExists("customers");
            }
        };

手順

seederファイルの生成

php artisan make:seeder CustomerSeeder

seederファイルに追記

database\seeders\CustomerSeeder.php
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use DateTime;

class CustomerSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        DB::table('customers')->insert(array(
            0 => 
            array (
                'name' => 'tanaka',
                'phone' => '07012345678',
                'created_at' => '2024-10-18 11:28:35', //固定の日時の場合
            ),
            1 => 
            array (
                'name' => 'yamada',
                'phone' => '08056781234',
                'created_at' => new DateTime(), //現在日時の場合
            ),
        ));
    }
}
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
    {
        $this->call([
            CustomerSeeder::class, //ここにseederファイルを追記
        ]);
    }
}

テーブルを再作成し、テストデータを投入

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?