### 模型工厂
database/factories/UserFactory.php
```
// 使用 factory 来创建一个 Faker\Generator 实例
$faker = Faker\Factory::create();
// 生成用户名
$faker->name; // "Janie Roob"
// 生成安全邮箱
$faker->safeEmail; // "claire.wuckert@example.net"
// 生成随机日期
$faker->date // "2011-02-10"
// 生成随机时间
$faker->time // "13:03:55"
```
## 数据填充
```
//定义用户模型工厂
$ php artisan make:seeder UsersTableSeeder
```
database/seeds/UsersTableSeeder.php
```
<?php
use Illuminate\Database\Seeder;
use App\Models\User;
class UsersTableSeeder extends Seeder
{
public function run()
{
$users = factory(User::class)->times(50)->make();
User::insert($users->makeVisible(['password', 'remember_token'])->toArray());
$user = User::find(1);
$user->name = 'Summer';
$user->email = 'summer@example.com';
$user->save();
}
}
```
database/seeds/DatabaseSeeder.php
```
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
public function run()
{
Model::unguard();
$this->call(UsersTableSeeder::class);
Model::reguard();
}
}
```
## 部署数据
```
//重置数据库
$ php artisan migrate:refresh
//执行数据填充
$ php artisan db:seed
//单独指定执行
$ php artisan db:seed --class=UsersTableSeeder
```