类型错误:传递给 Illuminate\Database\Grammar::parameterize

我正在使用 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();


    }

}


三国纷争
浏览 204回答 2
2回答

慕桂英546537

改变这一行:$answer_text = $faker->words;对此:$answer_text = $faker->word;所述words发生器的返回随机字阵列,始发该错误。

qq_笑_17

根据您的错误堆栈(调用堆栈):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值是一个单词而不是数组
打开App,查看更多内容
随时随地看视频慕课网APP