我正在使用Laravel工厂来播种数据库,但我不知道为什么会引发此错误。 (显示在底部)。
这是我的第一篇文章,所以请让我知道如何对这个问题进行更清晰的描述:)
PollAnswerFactory.php
$factory->define(PollAnswer::class, function (Faker\Generator $faker) {
$answer_text = $faker->word;
$answer_order = rand(0,10);
$question_id = rand(1,500);
return [
"answer_text" => $answer_text,
"answer_order" => $answer_order,
"question_id" => $question_id,
];
});
PollAnswer.php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class PollAnswer extends Model {
/**
* @var bool Indicates if the model should be timestamped.
*/
public $timestamps = false;
/**
* @var string The database table for this model.
*/
protected $table = 'poll_answers';
/**
* The connection name for the model.
*
* @var string
*/
protected $connection = 'poll_database';
/**
* The storage format of the model's date columns.
*
* @var string
*/
protected $dateFormat = 'U';
/**
* The primary key(s) for the poll_answers table
*
* @var string
*/
protected $primaryKey = 'answer_id';
/**
* @var array The fillable attributes of this model.
*/
protected $fillable = [
'answer_text',
'answer_order',
'question_id'
];
/**
* This establishes the eloquent one-to-many relationship
*
* @return HasMany
*/
public function poll_result() {
return $this->hasMany(PollResult::class);
}
/**
* This establishes the eloquent (inverse) one-to-many relationship
*
* @return BelongsTo
*/
public function poll_question() {
return $this->belongsTo(PollQuestion::class);
}
}
PollSeeder.php
use Illuminate\Database\Seeder;
class PollSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
factory(PollAnswer::class, 1500)->create();
}
}
PollController.php
public function index()
{
$seeder = new PollSeeder();
$seeder->run();
}
错误消息: (1/1)致命错误 类型错误:传递给Illuminate \ Database \ Grammar :: parameterize()的参数1必须为数组类型,给定整数,在/ var / www / vhosts / removed / www-root / core / library / vendor / laravel /中调用681行上的framework / src / Illuminate / Database / Query / Grammars / Grammar.php
调用堆栈:
at Grammar->parameterize(3)
in Grammar.php line 681
at Grammar->Illuminate\Database\Query\Grammars\{closure}(3, 'answer_order')
at array_map(object(Closure), array('answer_text' => array('soluta', 'doloribus', 'quis'), 'answer_order' => 3, 'question_id' => 256), array('answer_text', 'answer_order', 'question_id'))
in Collection.php line 861
at Collection->map(object(Closure))
in Grammar.php line 682
at Grammar->compileInsert(object(Builder), array('answer_text' => array('soluta', 'doloribus', 'quis'), 'answer_order' => 3, 'question_id' => 256))
in Grammar.php line 697
at Grammar->compileInsertGetId(object(Builder), array('answer_text' => array('soluta', 'doloribus', 'quis'), 'answer_order' => 3, 'question_id' => 256), 'answer_id')
in Builder.php line 2155
at Builder->insertGetId(array('answer_text' => array('soluta', 'doloribus', 'quis'), 'answer_order' => 3, 'question_id' => 256), 'answer_id')
in Builder.php line 1283
at Builder->__call('insertGetId', array(array('answer_text' => array('soluta', 'doloribus', 'quis'), 'answer_order' => 3, 'question_id' => 256), 'answer_id'))
in Model.php line 722
at Model->insertAndSetId(object(Builder), array('answer_text' => array('soluta', 'doloribus', 'quis'), 'answer_order' => 3, 'question_id' => 256))
in Model.php line 687
at Model->performInsert(object(Builder))
in Model.php line 550
at Model->save()
in FactoryBuilder.php line 172
at FactoryBuilder->Illuminate\Database\Eloquent\{closure}(object(PollAnswer), 0)
in Collection.php line 339
at Collection->each(object(Closure))
in FactoryBuilder.php line 173
at FactoryBuilder->store(object(Collection))
in FactoryBuilder.php line 153
at FactoryBuilder->create()
in PollSeeder.php line 31
at PollSeeder->run()
in PollController.php line 35
at PollController->index()
at call_user_func_array(array(object(PollController), 'index'), array())
in Controller.php line 54
at Controller->callAction('index', array())
in ControllerDispatcher.php line 45
at ControllerDispatcher->dispatch(object(Route), object(PollController), 'index')
in Route.php line 212
at Route->runController()
in Route.php line 169
at Route->run()
in Router.php line 658
at Router->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in RefreshToken.php line 84
at RefreshToken->handle(object(Request), object(Closure))
in Pipeline.php line 149
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Authenticate.php line 54
at Authenticate->handle(object(Request), object(Closure))
in Pipeline.php line 149
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure))
in Pipeline.php line 149
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ThrottleRequests.php line 57
at ThrottleRequests->handle(object(Request), object(Closure), 60, '1')
in Pipeline.php line 149
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php line 102
at Pipeline->then(object(Closure))
in Router.php line 660
at Router->runRouteWithinStack(object(Route), object(Request))
in Router.php line 635
at Router->runRoute(object(Request), object(Route))
in Router.php line 601
at Router->dispatchToRoute(object(Request))
in Router.php line 590
at Router->dispatch(object(Request))
in Kernel.php line 176
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php line 149
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php line 149
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure))
in Pipeline.php line 149
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
in Pipeline.php line 149
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php line 102
at Pipeline->then(object(Closure))
in Kernel.php line 151
at Kernel->sendRequestThroughRouter(object(Request))
in Kernel.php line 116
at Kernel->handle(object(Request))
in index.php line 49```
答案 0 :(得分:1)
根据您的错误堆栈(调用堆栈):
at Grammar->compileInsert(object(Builder), array('answer_text' => array('soluta', 'doloribus', 'quis'), 'answer_order' => 3, 'question_id' => 256))
您正试图在数组 answer_text 字段中插入一个数组值
array('answer_text' => array('soluta', 'doloribus', 'quis'), 'answer_order' => 3, 'question_id' => 256)
在您的代码中,您使用$ faker-> word来生成此数据,但是在为数据库设置种子时,将生成要插入的数组值。请再次检查数据库种子和工厂,并确保 answer_text 值是非数组
一词答案 1 :(得分:0)
更改此行:
$answer_text = $faker->words;
对此:
$answer_text = $faker->word;
words
生成器返回一个随机单词数组,该数组引发错误。