Laravelでseederを使おう

まず、seeder fileを作ります。

[vagrant@localhost test]$ php artisan make:seeder SmartTableSeeder
Seeder created successfully.

すると、database/seeds配下に SmartTableSeeder.php が出来る。

runメソッドでデータ挿入

use Illuminate\Database\Seeder;

class SmartTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $ministers = ['taro', 'masatoshi', 'takashi','masahiko','takumi'];
        foreach ($ministers as $minister) {
        	DB::table('minister')->('minister')
        }
    }
}

あれ、DB::table(‘minister’)->(‘minister’) って、なんかおかしいぞ。
で、シーダーファイルは、DatabaseSeeder.phpから呼び出す。

use Illuminate\Database\Seeder;

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

seeder 実行
[vagrant@localhost test]$ php artisan db:seed
Seeding: SmartTableSeeder::class

In Container.php line 729:

Class SmartTableSeeder::class does not exist

ん?
[vagrant@localhost test]$ php composer.phar dump-autoload
Generating optimized autoload filesCarbon 1 is deprecated, see how to migrate to Carbon 2.
https://carbon.nesbot.com/docs/#api-carbon-2
You can run ‘./vendor/bin/upgrade-carbon’ to get help in updating carbon and other frameworks and libraries that depend on it.
Generated optimized autoload files containing 3116 classes
[vagrant@localhost test]$ php artisan db:seed
Seeding: SmartTableSeeder::class

In Container.php line 729:

Class SmartTableSeeder::class does not exist

あれええええええー

あ、こうか

use Illuminate\Database\Seeder;


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

[vagrant@localhost test]$ php artisan db:seed
Seeding: SmartTableSeeder

In Connection.php line 647:

SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘test.minister’ d
oesn’t exist (SQL: insert into `minister` (`age`, `name`) values (54, taro)
, (54, taro))

In Connection.php line 445:

SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘test.minister’ d
oesn’t exist

うん、seederが複数あるときと、一つの場合だと書き方が異なりますね。
migrationしてないので、カラムが無いと表示されていますが、seederについてはわかりました。