Exemple #1
0
$dates = ['created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
// 用户
$factory->define(User::class, function (Generator $faker) use($dates) {
    return array_merge(['name' => $faker->unique()->name, 'email' => $faker->unique()->email, 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10)], $dates);
});
// 分类
$factory->define(Category::class, function (Generator $faker) use($dates) {
    return array_merge(['name' => $faker->unique()->word, 'slug' => $faker->word, 'desc' => $faker->paragraph], $dates);
});
// 期数
$factory->define(Issue::class, function (Generator $faker) use($dates) {
    return array_merge(['issue' => $faker->unique()->numberBetween(1, 30), 'published_at' => $faker->dateTimeThisYear], $dates);
});
// 文章
$factory->define(PublishingArticle::class, function (Generator $faker) use($dates) {
    $issues = Issue::lists('issue')->toArray();
    $categoryIds = Category::lists('id')->toArray();
    return array_merge(['issue' => $faker->randomElement($issues), 'category_id' => $faker->randomElement($categoryIds), 'title' => $faker->sentence(), 'desc' => $faker->paragraph, 'url' => $faker->url, 'presenter' => $faker->name], $dates);
});
// 投稿
$factory->define(ContributeArticle::class, function (Generator $faker) use($dates) {
    return array_merge(['title' => $faker->sentence(), 'desc' => $faker->paragraph, 'url' => $faker->url, 'presenter' => $faker->name], $dates);
});
// 文章标签
$factory->define(Tag::class, function (Generator $faker) use($dates) {
    return array_merge(['name' => $faker->unique()->word], $dates);
});
// 文章和标签之间的关联关系
$factory->define(Taggable::class, function (Generator $faker) use($dates) {
    $taggableIds = PublishingArticle::lists('id')->toArray();
    $tagIds = Tag::lists('id')->toArray();