When we create a good table structure, we usually have to generate some test data to test and deal with this scenario Laravel provides a decent service--seed
Laravel's seeder will be placed in the/database/seeders directory and will provide a databaseseeder in the Databaseseeder Run method you can call other seeder you create
1 Creating a Seeder
Run Artisan Create:
PHP Artisan Make:seeder Articlesseeder
1.1 Populating with builders
There is only one run method in the Seeder we can execute our insertion method in the Run method, which can be used by the builder in run or the Model factory (for the two-point follow-up will be written to OH)
Public function Run () { DB:: Table (' articles '),insert ([ ' title ' = = str_random), ' body ' = str_ Random (+), ]); }
1.2 Filling with model Factory (common)
Public function Run () { Factory (\app\user::class, ten),Create (); }
This creates a total of 10 users.
2 performing shims2.1 Specify Execution
Let's say we execute articlesseeder. This filler can be written like this:
PHP Artisan db:seed--class=articlesseeder
2.2 Default Execution
The default execution is to execute the Databaseseeder this shim:
PHP Artisan Db:seed
2.3 Calling a custom shim
If the Databaseseeder shim is executed, either implement the data population in its run method or call the custom shim:
class extends seeder{ * * *Run the database seeds. * * @return void */public function run () { Model::unguard (); $this->call (' Articlesseeder '); Model::reguard (); }}
Then run:
PHP Artisan Db:seed
Laravel5.1 Populating the Database