【问题标题】:Additive parameters in factory are added to table insert command工厂中的附加参数被添加到表插入命令中
【发布时间】: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 只是我想设置为工厂的参数。 怎么了?

【问题讨论】:

标签: php laravel laravel-5 factory


【解决方案1】:

如果我正确理解您的需求, 您希望能够使用与 Hostel 有关系的工厂创建 HostelReview

您传递给 factory(App\HostelReview::class, 10)->create([...]) 的参数将作为特定模型属性处理并覆盖工厂中的默认数据。 你需要的是:

  • 定义将附加到评论的默认旅馆
  • 能够在评论中注入特定的旅馆

这就是 HostelReview 的样子:

$factory->define(App\HostelReview::class, function (Faker $faker) {
    $flag_status= 'N';
    if( rand(1,4) == 1) {
        $flag_status= 'R';
    }

    return [

        'hostel_id'             => function () {
            return factory(App\Hostel::class)->create()->id; // This will create a new Hostel and set the id in case you did not express in the caller the hostel_id to inject
        },
        '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')  ) ,
    ];
});

对于第二点,您可以使用此调用覆盖默认附加的 Hostel:

factory(App\HostelReview::class, 10)->create(['hostel_id' => 30]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2020-10-22
    相关资源
    最近更新 更多