[Laravel5.1-0.0.6] Seeder automatically fills in test data
Book: http://www.jianshu.com/users/85c8826ce087/latest_articles
1. what is Seeder?
2. what is Seeder used?
Automatically generate batch test data;
After table migration, a series of data can be automatically imported for collaborative development;
3. original Seeder code
call(UserTableSeeder::class); Model::reguard(); }}
The DatabaseSeeder class contains only the run method, which is called when the data generation command is run;
The three database operation methods can be used in the run () method.
No return value
4. use Seeder 4.1 Basic use
Fill in the content in database/seeds/DatabaseSeeder. php to fill in the data;
The content added in the run () method will be filled with the data content in the articles table (using SQL statements ):
public function run() { DB::insert('insert into articles(title, content,created_at,updated_at) values (?, ?,?,?)', ['article-title2','article-content2', \Carbon\Carbon::now(),\Carbon\Carbon::now()] ); }
To generate multiple records at the same time, you can add multiple statements and then run the same command.
Php artisan db: seed;
A simple time package: Carbon is recommended;
4.2 generate the filler separately and use
php artisan make:seeder ArticleTableSeeder
Cut the insert statement previously written in the database/seeds/DatabaseSeeder class into the run () method of the database/seeds/ArticleTableSeeder class;
In the DatabaseSeeder class run () method, use the call method to obtain the padding class to run:
public function run(){ $this->call(ArticleTableSeeder::class);}
4.3 run the filler
php artisan db:seed
php artisan db:seed --class=ArticleTableSeeder
php artisan migrate:refresh --seed
4.4 faster batch production data Model factory
Learning Laravel school notes: http://laravelacademy.org/post/133.html