1. Introduction
Laravel contains a simple way to populate a database-using fill classes and test data. All fill classes are located in the Database/seeds directory. The class name of the fill class is completely customizable, but it's best to follow certain rules, such as readability, such as Usertableseeder, and so on. When Laravel is installed, a Databaseseeder class is provided by default. From this class, you can use the call method to run other fill classes, allowing you to control the fill order.
2. Write the filler
To generate a shim, you can make:seeder through the Artisan command. All framework-generated fillers are located in the Database/seeders directory:
PHP Artisan Make:seeder Usertableseeder
A shim class contains only one method by default: Run. This method is called when the artisan command Db:seed is run. In the Run method, you can insert any data you want to insert into the database, you can use the Query Builder to insert the data manually, or you can use the eloquent model factory.
For example, let's modify the Databaseseeder class that comes with the Laravel installation, adding a database insert statement to the Run method:
Insert ([ ' name ' = str_random, ' email ' = ' str_random (10) '. @gmail. com ', ' password ' = bcrypt (' secret '), ]); }
Using a model factory
Of course, it is cumbersome to manually specify the properties of each model fill, and instead we can use the Model factory to easily generate a large number of database records. First, look at the Model factory documentation to learn how to define the factory, define the factory, and use the Help function factory to insert the record into the database.
For example, let's create 50 users and add an association that relates to each user:
/** * Run Database population * * @return void */public function run () { factory (' App\user ', ')->create ()->each (function ($u) { c8/> $u->posts ()->save (Factory (' App\post ')->make ());} );
Call an extra shim
In the Databaseseeder class, you can use the call method to perform additional population classes, and using the call method allows you to break up a database population into multiple files, so that a single shim class will not become huge, simply pass the name of the shim class you want to run:
/*** Run Database Population * * @return Void*/public function run () { model::unguard (); $this->call (usertableseeder::class); $this->call (poststableseeder::class); $this->call (commentstableseeder::class); Model::reguard ();}
3. Run the Filler
After you have written the Shim class, you can use the Artisan command db:seed to populate the database. By default, the Db:seed command runs the Databaseseeder class that can be used to run other shim classes, but you can also use the--class option to specify a separate shim class that you want to run:
PHP artisan db:seedphp Artisan db:seed--class=usertableseeder
You can also populate the database with the Migrate:refresh command, which can also roll back and rerun the migration, which is useful when you need to fully rebuild the database:
PHP Artisan Migrate:refresh--seed