Laravel 数据填充 seed

参考文档: https://github.com/fzaninotto/Faker

设置数据填充规则

database\factories\ModelFactory.php 文件,添加给posts表填充数据规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$factory->define(App\Post::class, function (Faker\Generator $faker) {
return [
'title' => $faker->sentence(6),// 最多6个单词
'content' => $faker->paragraph(10),// 10条语句
];
});
```

## 填充10条数据并打印出来

在命令行使用tinker

```bash
$ php artisan tinker
Psy Shell v0.9.12 (PHP 7.2.1 — cli) by Justin Hileman

>>> factory(App\Post::class, 10)->make();

填充10条数据并插入数据库

在命令行使用tinker

1
2
3
4
$ php artisan tinker
Psy Shell v0.9.12 (PHP 7.2.1 — cli) by Justin Hileman

>>> factory(App\Post::class, 10)->create();
0%