4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelで初期データ投入

Last updated at Posted at 2020-06-12

目次

Laravelの記事一覧は下記
PHPフレームワークLaravelの使い方

Laravelバージョン

動作確認はLaravel Framework 7.19.1で行っています

前提条件

eclipseでLaravel開発環境を構築する。デバッグでブレークポイントをつけて止める。(WindowsもVagrantもdockerも)
本記事は上記が完了している前提で書かれています
プロジェクトの作成もapacheの設定も上記で行っています

Laravelでデータベースを扱う準備をする
Laravelでテーブル作成
本記事は上記が完了している前提で書かれています
上記記事で作成したデータベース、テーブルを使っていきます

シーダクラス生成

Laravelでは初期データ投入するクラスをシーダといいます
シーダクラスを作成しましょう
コマンドラインで
sample
php artisan make:seeder Table1Seeder
php artisan make:seeder Table2Seeder
php artisan make:seeder Table3Seeder
xdebugの設定をしているとeclipseが実行していいですかというプロンプトを出すのでOKを押します
eclipseプロジェクトを右クリック→リフレッシュ
/sample/database/seeds/Table1Seeder.phpが現れます
/sample/database/seeds/Table2Seeder.phpが現れます
/sample/database/seeds/Table3Seeder.phpが現れます

シーダクラス修正

(1) /sample/database/seeds/Table1Seeder.phpを下記に修正

Table1Seeder.php
<?php

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

class Table1Seeder extends Seeder
{
    public function run()
    {
        DB::table('table1')->insert([
            [
                'varchar_col' => 'varchar1',
                'int_col' => 1,
                'datetime_col' => '2000-01-01 00:00:01',
                'date_col' => '2000-01-01',
                'time_col' => '00:00:01',
            ],
            [
                'varchar_col' => 'varchar2',
                'int_col' => 2,
                'datetime_col' => '2000-01-02 00:00:02',
                'date_col' => '2000-01-02',
                'time_col' => '00:00:02',
            ],
            [
                'varchar_col' => 'varchar3',
                'int_col' => 3,
                'datetime_col' => '2000-01-03 00:00:03',
                'date_col' => '2000-01-03',
                'time_col' => '00:00:03',
            ],
            [
                'varchar_col' => 'varchar4',
                'int_col' => 4,
                'datetime_col' => '2000-01-04 00:00:04',
                'date_col' => '2000-01-04',
                'time_col' => '00:00:04',
            ],
            [
                'varchar_col' => 'varchar5',
                'int_col' => 5,
                'datetime_col' => '2000-01-05 00:00:05',
                'date_col' => '2000-01-05',
                'time_col' => '00:00:05',
            ]
        ]);
    }
}

(2) /sample/database/seeds/Table2Seeder.phpを下記に修正

Table2Seeder.php
<?php

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

class Table2Seeder extends Seeder
{
    public function run()
    {
        DB::table('table2')->insert([
            [
                'table1_id' => 1,
                'varchar_col' => 'varchar10',
                'int_col' => 10,
                'datetime_col' => '2001-01-01 01:00:01',
                'date_col' => '2001-01-01',
                'time_col' => '01:00:01',
            ],
            [
                'table1_id' => 1,
                'varchar_col' => 'varchar20',
                'int_col' => 20,
                'datetime_col' => '2001-01-02 01:00:02',
                'date_col' => '2001-01-02',
                'time_col' => '01:00:02',
            ],
            [
                'table1_id' => 1,
                'varchar_col' => 'varchar30',
                'int_col' => 30,
                'datetime_col' => '2001-01-03 01:00:03',
                'date_col' => '2001-01-03',
                'time_col' => '01:00:03',
            ],
            [
                'table1_id' => 2,
                'varchar_col' => 'varchar40',
                'int_col' => 40,
                'datetime_col' => '2001-01-04 01:00:04',
                'date_col' => '2001-01-04',
                'time_col' => '01:00:04',
            ],
            [
                'table1_id' => 2,
                'varchar_col' => 'varchar50',
                'int_col' => 50,
                'datetime_col' => '2001-01-05 01:00:05',
                'date_col' => '2001-01-05',
                'time_col' => '01:00:05',
            ]
        ]);
    }
}

(3) /sample/database/seeds/Table3Seeder.phpを下記に修正

Table3Seeder.php
<?php

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

class Table3Seeder extends Seeder
{
    public function run()
    {
        DB::table('table3')->insert([
            [
                'table2_id' => 1,
                'varchar_col' => 'varchar100',
                'int_col' => 100,
                'datetime_col' => '2002-01-01 02:00:01',
                'date_col' => '2002-01-01',
                'time_col' => '02:00:01',
                'create_datetime' => DB::raw('NOW()'),
                'update_datetime' => DB::raw('NOW()'),
            ],
            [
                'table2_id' => 1,
                'varchar_col' => 'varchar200',
                'int_col' => 200,
                'datetime_col' => '2002-01-02 02:00:02',
                'date_col' => '2002-01-02',
                'time_col' => '02:00:02',
                'create_datetime' => DB::raw('NOW()'),
                'update_datetime' => DB::raw('NOW()'),
            ],
            [
                'table2_id' => 2,
                'varchar_col' => 'varchar300',
                'int_col' => 300,
                'datetime_col' => '2002-01-03 02:00:03',
                'date_col' => '2002-01-03',
                'time_col' => '02:00:03',
                'create_datetime' => DB::raw('NOW()'),
                'update_datetime' => DB::raw('NOW()'),
            ],
            [
                'table2_id' => 2,
                'varchar_col' => 'varchar400',
                'int_col' => 400,
                'datetime_col' => '2002-01-04 02:00:04',
                'date_col' => '2002-01-04',
                'time_col' => '02:00:04',
                'create_datetime' => DB::raw('NOW()'),
                'update_datetime' => DB::raw('NOW()'),
            ],
            [
                'table2_id' => 3,
                'varchar_col' => 'varchar500',
                'int_col' => 500,
                'datetime_col' => '2002-01-05 02:00:05',
                'date_col' => '2002-01-05',
                'time_col' => '02:00:05',
                'create_datetime' => DB::raw('NOW()'),
                'update_datetime' => DB::raw('NOW()'),
            ]
        ]);
    }
}

runメソッドだけが定義されていますが、runメソッドはシーダが実行されたときに呼び出られるメソッドです
今回runメソッドの中では5件のinsertが行われています
DB::table(xxx)->insertでテーブルにinsertができます
Laravel 7.x データベース:クエリビルダ INSERT

(4) /sample/database/seeds/DatabaseSeeder.phpのrunメソッドを修正

DatabaseSeeder.php
    public function run()
    {
        $this->call([
            Table1Seeder::class,
            Table2Seeder::class,
            Table3Seeder::class,
        ]);
    }

DatabaseSeederのrunメソッドで先ほど作成した3つのシーダを呼びます

シーダ実行

コマンドラインで
composer dump-autoload
シーダクラスを作成したら、dump-autoloadコマンドを実行する必要があります。
php artisan db:seed
これでシーダ実行です。これでdatabase/seeds/DatabaseSeeder.phpのrunメソッドが実行されます
xdebugの設定をしているとeclipseが実行していいですかというプロンプトを出すのでOKを押します

確認

MySQLでlaravel_sampleデータベースを確認してみましょう

select * from table1;
+----+-------------+---------+---------------------+------------+----------+
| id | varchar_col | int_col | datetime_col        | date_col   | time_col |
+----+-------------+---------+---------------------+------------+----------+
|  1 | varchar1    |       1 | 2000-01-01 00:00:01 | 2000-01-01 | 00:00:01 |
|  2 | varchar2    |       2 | 2000-01-02 00:00:02 | 2000-01-02 | 00:00:02 |
|  3 | varchar3    |       3 | 2000-01-03 00:00:03 | 2000-01-03 | 00:00:03 |
|  4 | varchar4    |       4 | 2000-01-04 00:00:04 | 2000-01-04 | 00:00:04 |
|  5 | varchar5    |       5 | 2000-01-05 00:00:05 | 2000-01-05 | 00:00:05 |
+----+-------------+---------+---------------------+------------+----------+
5 rows in set (0.000 sec)

select * from table2;
+----+-----------+-------------+---------+---------------------+------------+----------+
| id | table1_id | varchar_col | int_col | datetime_col        | date_col   | time_col |
+----+-----------+-------------+---------+---------------------+------------+----------+
|  1 |         1 | varchar10   |      10 | 2001-01-01 01:00:01 | 2001-01-01 | 01:00:01 |
|  2 |         1 | varchar20   |      20 | 2001-01-02 01:00:02 | 2001-01-02 | 01:00:02 |
|  3 |         1 | varchar30   |      30 | 2001-01-03 01:00:03 | 2001-01-03 | 01:00:03 |
|  4 |         2 | varchar40   |      40 | 2001-01-04 01:00:04 | 2001-01-04 | 01:00:04 |
|  5 |         2 | varchar50   |      50 | 2001-01-05 01:00:05 | 2001-01-05 | 01:00:05 |
+----+-----------+-------------+---------+---------------------+------------+----------+
5 rows in set (0.000 sec)

select * from table3;
+----+-----------+-------------+---------+---------------------+------------+----------+---------------------+---------------------+
| id | table2_id | varchar_col | int_col | datetime_col        | date_col   | time_col | create_datetime     | update_datetime     |
+----+-----------+-------------+---------+---------------------+------------+----------+---------------------+---------------------+
|  1 |         1 | varchar100  |     100 | 2002-01-01 02:00:01 | 2002-01-01 | 02:00:01 | 2020-06-17 01:00:00 | 2020-06-17 01:00:00 |
|  2 |         1 | varchar200  |     200 | 2002-01-02 02:00:02 | 2002-01-02 | 02:00:02 | 2020-06-17 01:00:00 | 2020-06-17 01:00:00 |
|  3 |         2 | varchar300  |     300 | 2002-01-03 02:00:03 | 2002-01-03 | 02:00:03 | 2020-06-17 01:00:00 | 2020-06-17 01:00:00 |
|  4 |         2 | varchar400  |     400 | 2002-01-04 02:00:04 | 2002-01-04 | 02:00:04 | 2020-06-17 01:00:00 | 2020-06-17 01:00:00 |
|  5 |         3 | varchar500  |     500 | 2002-01-05 02:00:05 | 2002-01-05 | 02:00:05 | 2020-06-17 01:00:00 | 2020-06-17 01:00:00 |
+----+-----------+-------------+---------+---------------------+------------+----------+---------------------+---------------------+
5 rows in set (0.000 sec)

レコードが投入されました

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?