19
18

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 5 years have passed since last update.

[ Laravel ] Seedingで楽々テストデータ挿入

Posted at

Laravelでは、seedingクラスを使うことで、初期テストデータを楽々入れることができます。

今日はUserデータを50件くらい作ってしまいます。

facker:テストデータ生成のphpライブラリ

Fackerはテストのフェイクデータをつくってくれるphpライブラリです。
fzaninotto/Faker: Faker is a PHP library that generates fake data for you

Database:Seeding:ララベルにある seed class

このライブラリとLaravelにあるseedingで楽々テストデータを作ることができます。
Database: Seeding - Laravel - The PHP Framework For Web Artisans

Userテーブル

今回データを入れるテーブル


mysql> desc users;
+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id             | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| facebook_id    | varchar(255)     | NO   | UNI | NULL    |                |
| name           | varchar(255)     | NO   |     | NULL    |                |
| email          | varchar(255)     | NO   | UNI | NULL    |                |
| password       | varchar(60)      | NO   |     | NULL    |                |
| remember_token | varchar(100)     | YES  |     | NULL    |                |
| created_at     | timestamp        | YES  |     | NULL    |                |
| updated_at     | timestamp        | YES  |     | NULL    |                |
+----------------+------------------+------+-----+---------+----------------+
8 rows in set (0.01 sec)

ディレクトリ構成と今回使うプログラム

app
├── User.php
database/
├── factories
│   └── UserFactory.php
├── migrations
└── seeds
    ├── DatabaseSeeder.php
    └── UsersTableSeeder.php

>> seeds/UsersTableSeeder.php

50件のデータを作成します。
モデルクラスとして渡すのはUser.phpです。

<?php

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\User::class, 50)->create();

    }
}

>> seeds/DatabaseSeeder.php

php artisan db:seedで実行した時に実行されるクラスです


<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //$this->call(ComposersTableSeeder::class);
        $this->call(UsersTableSeeder::class);
    }
}

>> database/factories/UserFactory.php



<?php

/*
|--------------------------------------------------------------------------
| User Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/

$factory->define(App\User::class, function (Faker\Generator $faker) {

    return [
        'facebook_id'=>$faker->uuid,
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => $faker->password,
        'remember_token' => $faker->md5,
        'created_at' => $faker->date,
        'updated_at' => $faker->date
    ];
});

>> 実行

上記のクラスを書き終えたら、以下のコマンドを実行するだけで、50件のテストデータを作ることができます。

php artisan db:seed
19
18
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
19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?