gpt4 book ai didi

php - Laravel 生成器给出 : Cannot redeclare generatorFunction()

转载 作者:行者123 更新时间:2023-12-02 07:19:54 26 4
gpt4 key购买 nike

我有以下代码,但是当我运行我的工厂时出现以下异常:

Cannot redeclare questionIndex() (previously declared in /Users/user/Desktop/my-app/database/factories/QuestionFactory.php:42) in /Users/user/Desktop/my-app/database/factories/QuestionFactory.php on line 46

当我运行我的单元测试时会发生这种情况,而这个特定的工厂现在不在测试中。我有其他工厂有一个生成器,但函数的名称完全不同。称为 autoIncrement()

<?php

use Faker\Generator as Faker;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

$questionIndex = questionIndex();

$factory->define(App\Models\Question::class, function (Faker $faker, $overrides) use ($questionIndex) {

$question = [
'How is 2 + 2?',
'Choose, what is the possibility to win the lottery?',
'According to the Big Brother, is 2 + 2 = 5?'
];

$questionType = [
'numeric',
'multiple-choice',
'true-or-false'
];

$index = $questionIndex->current();
$questionIndex->next();
return [
'title' => $question[$index],
'type' => $questionType[$index],
'category_id' => $overrides['category_id']
];

});

function questionIndex() {
for ($i = 0; $i < 100000; $i++) {
yield $i%3;
}
}

最佳答案

尝试使用函数声明屏蔽/保护以防文件被包含两次,然后函数将不会被重新声明。

if (!function_exists('questionIndex')) {
function questionIndex() {
for ($i = 0; $i < 100000; $i++) {
yield $i%3;
}
}
}

关于php - Laravel 生成器给出 : Cannot redeclare generatorFunction(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49165840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com