【问题标题】:in laravel 8 model factory gives array error在 laravel 8 模型工厂中给出数组错误
【发布时间】:2021-03-07 19:26:42
【问题描述】:

我是 laravel 初学者,想创建一个模型工厂。 RegulationFactory

我使用工厂和数据库播种器来制作它们。

当我想播种时,给出错误:

ErrorException : Array to string conversion

你能帮我解决它吗?

这是我的法规迁移:

public function up()
    {
        Schema::create('regulations', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');

            $table->string('country');
            $table->string('photo');
            $table->string('short_description');
            $table->longText('description');
            $table->string('population');
            $table->string('area');
            $table->string('internet_penetration');
            $table->string('national_currency');
            $table->string('goverment');
            $table->string('president');
            $table->string('capital');
            $table->string('language');
            $table->float('economic_growth');
            $table->string('dgtl_curr_lgs')
            ->comment('Legislation of digital currencies');

            $table->string('dgtl_curr_tax')
            ->comment('Tax on digital currencies');

            $table->string('dgtl_curr_pymt')
            ->comment('Payment status through digital currency');

            $table->string('dgtl_curr_ntiol')
            ->comment('National Digital Currency');

            $table->string('ICO');

            $table->string('crpto_antimon_rules')
            ->comment('has Anti-money laundering rules for crypto or not');

            $table->tinyInteger('status')
            ->comment('status is 1 when a regulation is active and it is 0 otherwise.')->nullable();

            $table->foreign('user_id')->references('id')->on('users');

            $table->rememberToken();
            $table->softDeletes();
            $table->timestamps();
        });
    }

这是我的调节工厂:

public function definition()
    {
        $users = User::pluck('id');


        return [
            'user_id'=>\App\Models\User::inRandomOrder()->first()->id,
            'country'=>$this->faker->country,
            'photo'=>$this->faker->text(10),
            'description'=>$this->faker->sentences(50),
            'short_description'=>$this->faker->sentence(50),
            'population'=>$this->faker->numerify('########'),
            'area'=>$this->faker->numerify('########'),
            'internet_penetration'=>$this->faker->numerify('#########'),
            'national_currency'=>$this->faker->word,
            'goverment'=>$this->faker->words,
            'president'=>$this->faker->name,
            'capital'=>$this->faker->city,
            'language'=>$this->faker->words,
            'economic_growth'=>$this->faker->numerify('#'),
            'dgtl_curr_lgs'=>$this->faker->sentence(1),
            'dgtl_curr_tax'=>$this->faker->words,
            'dgtl_curr_pymt'=>$this->faker->words,
            'dgtl_curr_ntiol'=>$this->faker->words,
            'ICO'=>$this->faker->word,
            'crpto_antimon_rules'=>$this->faker->word,
        ];
    }

还有我的监管模式:


class Regulation extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id' ,
        'country' ,
        'photo',
        'short_description' ,
        'description',
        'area',
        'internet_penetration',
        'national_currency',
        'goverment',
        'president',
        'capital',
        'language',
        'economic_growth',
        'dgtl_curr_lgs',
        'dgtl_curr_tax',
        'dgtl_curr_pymt',
        'dgtl_curr_ntiol',
        'ICO',
        'crpto_antimon_rules',
    ];

    public function users(){
        return $this->belongsTo(User::class);
    }
}

我的错误在哪里?谢谢你的帮助:}

【问题讨论】:

    标签: php factory laravel-8


    【解决方案1】:

    您的错误是您使用的是$this->faker->words。应该是$this->faker->word(不带“s”)

    见:Faker Provider Lorem

    【讨论】:

      【解决方案2】:

      正如你所使用的 $this->faker->words。在这里,$this->faker->words(5) 需要多个单词。所以,你需要通过传递值来设置单词的数量,或者你只能使用“$this->faker->word”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-26
        • 2016-10-17
        • 2020-12-28
        • 2021-01-04
        • 2021-11-26
        • 2021-01-14
        • 2021-04-17
        相关资源
        最近更新 更多