PHP 12 - Laravel Blog (Seeder & Factory)

By Sheldon L Published at 2020-09-11 Updated at 2020-09-11


Seeder

composer require laravel/helpers
php artisan make:seeder CategoryTableSeeder
php artisan make:seeder AuthorTableSeeder
# CategoryTableSeeder
public function run()
{
    Category::create([
        'admin_id' => '1',
        'name' => ['cn'=>'未分类', 'en'=>'uncategorized'],
        'slug' => 'uncategorized',
        'is_published' => '1',
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ]);
    Category::create([
        'admin_id' => '1',
        'name' => ['cn'=>'南德理论', 'en'=>'Land Theory'],
        'slug' => 'land-theory',
        'is_published' => '1',
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ]);
    Category::create([
        'admin_id' => '1',
        'name' => ['cn'=>'南德往事', 'en'=>'Land History'],
        'slug' => 'land-history',
        'is_published' => '1',
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ]);
    Category::create([
        'admin_id' => '1',
        'name' => ['cn'=>'万尾鲨鱼苗', 'en'=>'Shark Pups'],
        'slug' => 'shark-pups',
        'is_published' => '1',
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ]);
    Category::create([
        'admin_id' => '1',
        'name' => ['cn'=>'长江计划', 'en'=>'Yangtze Project'],
        'slug' => 'yangtze-project',
        'is_published' => '1',
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ]);
}

# AuthorTableSeeder
public function run()
{
    Author::create([
        'admin_id' => '1',
        'name' => ['cn'=>'牟其中', 'en'=>'Qizhong Mou'],
        'slug' => 'qizhong-mou',
        'is_published' => '1',
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ]);
    Author::create([
        'admin_id' => '1',
        'name' => ['cn'=>'总裁办', 'en'=>"CEO Office"],
        'slug' => 'ceo-office',
        'is_published' => '1',
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ]);
}

php artisan db:seed --class=CategoryTableSeeder
php artisan db:seed --class=AuthorTableSeeder

Factory

# UserFactory.php

$factory->define(Post::class, function (Faker $faker) {
    $title = $faker->unique()->sentence;
    $isPublished = ['1', '0'];

    return [
        'admin_id' => '1',
        'title' => ['cn' => rand(1, 10000).'中文标题'.rand(1, 10000), 'en' => $title],
        'slug' => str_slug($title),
        'sub_title' => ['cn' => rand(1, 10000).'中文副标题'.rand(1, 10000), 'en' => $faker->sentence],
        'is_reproduced' => '0',
        'intro' => ['cn' => rand(1, 10000).'中文简介'.rand(1, 10000), 'en' => $faker->paragraph],
        'details' => ['cn' => rand(1, 10000).'中文内容'.rand(1, 10000), 'en' => $faker->paragraph],
        'post_type' => 'post',
        'is_published' => $isPublished[rand(0, 1)],
        'created_at' => now(),
        'updated_at' => now(),
    ];
});

$factory->define(CategoryPost::class, function (Faker $faker) {
    return [
        'category_id' => rand(1, 5),
        'post_id' => rand(1, 20),
    ];
});
php artisan tinker
factory(App\Post::class, 20)->create()
factory(App\CategoryPost::class, 20)->create()