使用Laravel 5.5模型工厂播种数据透视表-mb_strtolower()期望参数1为字符串,给定数组 [英] Seeding pivot table with Laravel 5.5 model factory - mb_strtolower() expects parameter 1 to be string, array given

查看:164
本文介绍了使用Laravel 5.5模型工厂播种数据透视表-mb_strtolower()期望参数1为字符串,给定数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我有以下表格:

  • 用户
  • 问题
  • 标签
  • question_tag my pivot table with two fields: question_id & tag_id
  • users
  • questions
  • tags
  • question_tag my pivot table with two fields: question_id & tag_id

和我的 App \ Question 模型具有以下关系:

and my App\Question model has the following relationships:

class Question extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function tags()
    {
        return $this->hasMany(Tag::class);
    }
}

我创建了以下工厂:

database/factories/UserFactory.php

$factory->define(App\User::class, function (Faker $faker) {
    static $password;

    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('123456')
    ];
});

database/factories/QuestionFactory.php

$factory->define(App\Question::class, function (Faker $faker) {
    static $user_id;

    return [
        'user_id' => $user_id,
        'subject' => $faker->sentence(15),
        'body' => $faker->paragraph(10)
    ];
});

database/factories/TagFactory.php

$factory->define(App\Tag::class, function (Faker $faker) {
    return [
        'name' => str_slug($faker->words(mt_rand(1, 2))),
        'description' => $faker->sentence()
    ];
});

我试图像这样在虚拟数据播种器中一起使用它们:

I tried to use it all together in my dummy data seeder like this:

class DummyDataSeeder extends Seeder
{
    public function run()
    {
        // Seed dummy users
        factory(App\User::class, 10)->create()->each(function($user)
        {
            // With dummy questions
            $user->questions()->saveMany(factory(App\Question::class, 10)->make()->each(function($question)
            {
                // With dummy tags
                $question->tags()->sync(factory(App\Tag::class, 3)->make());
            }));
        });
    }
}

运行播种机时,出现以下错误:

When I run the seeder, I get the following error:

[ErrorException] mb_strtolower()期望参数1为字符串, 给定数组

[ErrorException] mb_strtolower() expects parameter 1 to be string, array given

在模型工厂中这不可能吗?我需要使用其他方法吗?

Is this not possible to do in model factory? Do I need to use a different approach?

推荐答案

我认为您需要对数据透视表使用belongsToMany

I think you need to use belongsToMany for your pivot Table

在您的问题模型中

public function tags()
 {
  return $this->belongsToMany(Tag::class,'question_tag','tag_id','question_id');
 }

与您的标记模型相同

public function questions()
{
 return $this->belongsToMany(Question::class,'question_tag','question_id','tag_id');
}

另外,您需要更改

 $question->tags()->sync(factory(App\Tag::class, 3)->make());

$tags = factory(App\Tag::class, 3)->make();
$tagIds = Tag::select('id')->get()->toArray();
 $question->tags()->sync($tagIds);

这意味着您需要在Sync参数中传递ID.

which means you need to pass IDs in Sync argument.

这篇关于使用Laravel 5.5模型工厂播种数据透视表-mb_strtolower()期望参数1为字符串,给定数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆