使用工厂为数据库植入种子时,使用Laravel中的PHP Faker生成“唯一的”条目

鲁宾逊

因此,类似于带有验证的唯一规则(请参阅:https : //github.com/felixkiss/uniquewith-validator),我想知道如何生成一个条目,其中一列与另一列是唯一的。我想按如下方式播种数据库。

例:

“步骤”表中有12个步骤。每个步骤应具有与每个步骤相关联的5个类别,这些类别存储在“ step_categories”表中。这些类别中的每一个都被分配一个唯一的订单号1到5,这对于每个“ step_id”都是唯一的。

请参见此图像,以获取数据库外观的示例:https : //imgur.com/a/XYA5yyn

对于上述图像示例,我不得不手动在数据库中进行输入。我不想每次都手动生成它,例如说我犯了一个错误,而不必回滚迁移。

我正在使用工厂生成此数据。所以工厂名称是StepCategoriesFactory.php,显然我正在使用文件中create()方法调用工厂DatabaseSeeder.php

我考虑过要在for循环中执行此操作,然后我才意识到当我调用the'step_id' => App\Model::all()->random()->id来获取新ID时,我无法确保我没有获取我刚刚为其生成5个条目的ID 。我对Laravel真的很陌生,我不确定从哪里开始。没有关于SO的真实信息,伪造者可以在另一列中使用唯一性。我将如何处理?

注意:步骤ID并不总是会是1-12。步骤ID可能会有所不同,具体取决于是否删除并重新制作了步骤。因此,仅将分配step_id为等于1-12不会起作用。

更新:这是我刚刚编写的一些代码,我认为自己的方向正确。也许。我已经step_idnumber字段抓取了,因为它始终是1-12,并且我已经从条目中抓取了IID。但是,现在我停留在如何生成订单1-5而不重复自身的问题上。我还没有运行它,因为它不完整,我知道如果没有正确的订单号就会抛出错误。

更新2:我认为我在这里是对的。但是我收到一个未定义的变量错误。当我从匿名函数中放入第一行时,它会将每个条目的顺序重置为“ 1”。如何使$ autoIncrement变量可用于匿名函数?Seeder在两次更新之间保持不变。

错误图片:https : //imgur.com/a/ywkd0Lb终端中带有Die / Dump错误的第二张图片:https : //imgur.com/a/rOGRv32

在此处引用此文章:https : //laracasts.com/discuss/channels/laravel/model-factory-increment-value-faker?page=1

更新3:我忘记use ($autoIncrement)了匿名函数的代码行。下面的代码已更新,但是现在我收到另一个错误,指出订单列具有空值,无法插入。显然,它应该为“ 1”。即使我调用了将$autoIncrement->next();其递增为“ 1”的my ,它仍然会根据终端返回null。但是,当我对它进行一次死注时,$autoIncrement->current()它返回1.奇怪。

更新3错误:https : //imgur.com/a/STOmIjF

StepCategoriesFactory.php

use Faker\Generator as Faker;

$autoIncrement = autoIncrement();

$factory->define(App\StepCategory::class, function (Faker $faker) use ($autoIncrement) {
    // Generate Created At and Updated at DATETIME
    $DateTime = $faker->dateTime($max = 'now');
    $autoIncrement->next();
    $order = (int) $autoIncrement->current();

    return [
        // Generate Dummy Data
        'order' =>  $order,
        'name' => $faker->words(4, true),
        'created_at' => $DateTime,
        'updated_at' => $DateTime,
    ];
});

function autoIncrement()
{
    for ($i = 0; $i < 5; $i++) {
        yield $i;
    }
}

编辑:悬赏这个问题,因为我认为这对社区获得详细的答案将有所帮助。我正在寻找帮助来解释如何确保在每个循环中都获取相同的条目。

鲁宾逊

终于解决了!

因此,我接受了每个人的答案,并认真思考了如何使用for循环创建订单号。1-5。我最后遇到的问题是$ i变量未重置。因此,在收益率之后,我必须检查$ i变量是否等于5,然后将其重置为零。

继承人的代码!

StepCategories.php

use Faker\Generator as Faker;

$autoIncrement = autoIncrement();

$factory->define(App\StepCategory::class, function (Faker $faker) use ($autoIncrement) {
    // Generate Created At and Updated at DATETIME
    $DateTime = $faker->dateTime($max = 'now');

    // Get the next iteration of the autoIncrement Function
    $autoIncrement->next();
    // Assign the current $i value to a typecast variable.
    $order = (int) $autoIncrement->current();


    return [
        // Generate Dummy Data
        'order' =>  $order,
        'name' => $faker->words(4, true),
        'created_at' => $DateTime,
        'updated_at' => $DateTime,
    ];
});

function autoIncrement()
{
    // Start a loop
    for ($i = 0; $i <= 5; $i++) {
        // Yield the current value of $i
        yield $i;
        // If $i is equal to 5, that must mean the start of a new loop
        if($i == 5) {
            // Reset $i to 0 to start over.
            $i = 0;
        }
    }
}

DatabaseSeeder.php

// Generate Dummy Categories
// Run the factory 12 times
foreach(range(1, 12) as $i) {
    // Generate 5 entries each time
    factory(App\StepCategory::class, 5)->create([
        // Since all steps have a number 1-12 grab the step by the number column and get it's ID
        'step_id' => App\Step::where('number', '=', $i)->first()->id,
    ]);
}

感谢所有的帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章