laraver6-数据填充
1.创建模型工厂 php artisan make:factory AdminFactory --model=Admin
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Models\System\Admin;
use Faker\Generator as Faker;
$factory->define(Admin::class, function (Faker $faker) {
return [
'name' => $faker->name,
'account' => $faker->unique()->name,
'password' => bcrypt(Admin::$defaultPassword),
];
});
2.创建seeders php artisan make:seeder PostTableSeeder
完成 seeder 类的编写之后,你可能需要使用dump-autoload
命令重新生成 Composer 的自动加载器
<?php
use Illuminate\Database\Seeder;
use App\Models\System\Admin;
class AdminTableSeeder extends Seeder
{
public function run()
{
factory(Admin::class, 2)->create();
}
}
3. 将新加的seeder添加到DatabaseSeeder.php <?php use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { public function run() { $this->call([ AdminTableSeeder::class ]); } }
4.执行命令 php artisan db:seed php artisan db:seed --class=AdminTableSeeder //指定seeder