我正在使用 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();
}
}
慕桂英546537
qq_笑_17