writing shims
PHP Artisan Make:seeder Usertableseeder
Modify the class that comes with the Laravel installation DatabaseSeeder
, adding a database insert statement to the run
method:
<?phpuse db;use illuminate\database\seeder;use Illuminate\database\eloquent\model;classDatabaseseeder extends seeder{/** * Run Database population * * @return void*/ Publicfunction Run () {db::table ('Users'),Insert (['name'= Str_random (Ten), 'Email'= Str_random (Ten).'@gmail. com', 'Password'= Bcrypt ('Secret'), ]); }}
Using a model factory
Use the Help function factory
to insert a record into the database.
Create 50 users and add an association to each user:
Public function Run () { factory ('app\user')->create () Each (function ($u) { $u->posts ()->save (Factory ('app\post') Make ()); });}
Call an extra shim
In the DatabaseSeeder
class, use call
the method to perform an additional fill class
Public function Run () { model::unguard (); $This->call (usertableseeder::class); $This->call (poststableseeder::class); $This->call (commentstableseeder::class);}
Run the Filler
--class=usertableseeder
You can also migrate:refresh
populate the database with commands, which can also be rolled back and rerun the migration, which is useful when you need to fully rebuild the database:
PHP Artisan Migrate:refresh--seed
Laravel Data population