쉽습니다. 그냥 공장을 정의하십시오.
[email protected] ~/demo> php artisan tinker
Psy Shell v0.8.1 (PHP 7.1.8 — cli) by Justin Hileman
>>> $user = factory(App\User::class)->make()
=> App\User {#880
name: "Jessy Doyle",
email: "[email protected]",
}
>>> $user = factory(App\User::class)->make()
=> App\User {#882
name: "Jessy Doyle",
email: "[email protected]",
}
Laravel 문서 : 그런 다음 그것을 테스트 어설프게을 사용
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
// Add this line to original factory shipped with laravel.
$faker->seed(123);
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});
데이터베이스/공장/ModelFacotry.php : 자 보자 기본 공장에서 보면 laravel 5.5
파일을 을 제공 :
how to define and use factory
Seeding
예상대로 작동하지 않았습니다. 팩토리의 인스턴스를 두 번 이상 생성 할 때 고유 옵션을 사용하지 않는 한 동일한 이름과 전자 메일을 사용하여 동일한 사용자를 생성합니다. 이것을 피할 수있는 방법이 있습니까? –
이전 주석에서 계속하려면 unique()을 사용하더라도 동일한 항목을 생성하지 않습니다. ( –
@PetarVasilev 늦은 시간에 죄송합니다. unique()이 없으면 동일한 faker 데이터를 얻게됩니다. –