Model Factory - 데이터 심기(Faker)

Admin 2020.04.19 23:18 조회 수 : 74

/database/factories/UsrFactory.php

 

$post = App\Posts::latest()->first();

$autoIncrement = autoIncrement($post->id);

$factory->define(App\Posts::class, function (Faker $faker) use ($autoIncrement) {

    $autoIncrement->next();

    return [

        'title'   => $faker->sentence,

        'content'    => $faker->paragraph,

        'pure_content'    => strip_tags($faker->paragraph),

        'user_id' => App\User::all()->random()->id,

        'remember_token' => Str::random(10),

        'list_order'    => -($autoIncrement->current()),

        'update_order'    => -($autoIncrement->current())

    ];

});

 

function autoIncrement($i = 0)

{

    for ($i; $i < 100000; $i++) {

        yield $i;

    }

}

 

 

사용

# php artisan tinker

# factory(App\Posts::class, 5)->create();