【发布时间】:2020-03-10 11:26:46
【问题描述】:
在我的 Laravel 5.8 应用程序中,我在 database/factories/HostelReviewFactory.php 中创建了工厂:
$factory->define(App\HostelReview::class, function (Faker $faker, $parentParams) {
$flag_status= 'N';
if( rand(1,4) == 1) {
$flag_status= 'R';
}
$parent_hostel_id= $parentParams['parent_hostel_id'];
return [
'hostel_id' => $parent_hostel_id,
'email_inquiried' => $faker->safeEmail,
'full_name' => $faker->name,
'status' => 'A',
'flag_status' => $flag_status,
'review' => $faker->text,
'stars_rating_type_id' => rand(1,5),
'created_at' => $faker->dateTimeBetween( '-2 years', 'now', config('app.timezone') ) ,
];
});
并从播种器数据库/种子/HostelReviewsTableSeeder.php 运行它:
factory(App\HostelReview::class, 10)->create([ 'parent_hostel_id' => 30 ]);
我有错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'parent_hostel_id' in 'field list' (SQL: insert into `ad_hostel_reviews` (`hostel_id`, `email_inquiried`, `full_name`, `status`, `flag_status`, `review`, `stars_rating_type_id`, `created_at`, `parent_hostel_id`) values (30, beer.ozella@example.com, Jamel Konopelski, A, N, Quis mollitia voluptas occaecati corrupti ut. Commodi dolorem delectus architecto nesciunt voluptatem quos. Itaque natus adipisci dicta impedit sint. Alias inventore accusantium ea., 3, 2018-02-13 01:18:34, 30))
看起来来自 $parentParams 的所有值都添加到目标表的字段列表中,我不需要它,因为 $parentParams 只是我想设置为工厂的参数。 怎么了?
【问题讨论】:
-
你的桌子上是否有“parent_hostel_id”这一列?
标签: php laravel laravel-5 factory