Laravel:有几个种子表属于使用工厂的关系

长话短说:


我正在使用 Laravel 和 Eloquent 并尝试为数据库播种并将多个属于关系附加到一个表,但出现错误:


调用未定义的方法 Illuminate\Database\Eloquent\Relations\BelongsTo::attach()


详细解释:


我正在使用php 7.1和Laravel 6雄辩。我正在尝试为测试数据库播种。我有以下关系结构:


一个用户有多个帖子/一个帖子属于一个用户


一个类别有多个帖子/一个帖子属于一个类别


用户模型:


class User extends Model

{

    protected $table = 'users';


    public function posts()

    {

        return $this->hasMany('App\Models\Post');

    }

品类型号:


class Category extends Model

{

    protected $table = 'categories';


    public function posts()

    {

        return $this->hasMany('App\Models\Category');

    }

}

帖子模型:


class Post extends Model

{

    protected $table = 'posts';


    public function category()

    {

        return $this->belongsTo('App\Models\Category');

    }


    public function user()

    {

        return $this->belongsTo('App\Models\User');

    }

}

用户工厂:


$factory->define(User::class, function (Faker $faker) {

    return [

        'id' => $faker->unique()->randomNumber(3),

        'name' => $faker->name(),

    ];

});

分类工厂:


$factory->define(Category::class, function (Faker $faker) {

    return [

        'id' => $faker->unique()->randomNumber(3),

        'name' => $faker->word(),

    ];

});

邮政工厂:


$factory->define(User::class, function (Faker $faker) {

    return [

        'id' => $faker->unique()->randomNumber(3),

        'name' => $faker->realText($maxNbChars = 200, $indexSize = 2),

    ];

});

如果我只有用户和帖子,我知道我可以通过以下方式为数据库播种:


public function seed()

{

    $users = factory(App\User::class, 10)

        ->create()

        ->each(function ($user) {

            $user->posts()->createMany(factory(App\Post::class, 10)->make()->toArray());

        });

}

但显然这对我来说行不通。我尝试了以下方法:


public function seed()

{

    factory(User::class, 10)->create();

    factory(Category::class, 10)->create();

    factory(Post::class, 100)->create()->each(function ($post){

        $post->user()->attach(User::all()->random(1));

        $post->category()->attach(Category::all()->random(1));

    });

}


但在这两种情况下,我都收到错误:


调用未定义的方法 Illuminate\Database\Eloquent\Relations\BelongsTo::attach()


梦里花落0921
浏览 87回答 1
1回答

慕田峪9158850

你必须使用associate():public function seed(){    factory(User::class, 10)->create();    factory(Category::class, 10)->create();    factory(Post::class, 100)->make()->each(function ($post) {        $post->user()->associate(User::inRandomOrder()->first());        $post->category()->associate(Category::inRandomOrder()->first());        $post->save();    });}还将User::all()->random(1)返回一个集合而不是模型,这将引发异常,我将其替换为Model::inRandomOrder()->first(),它将从数据库中获取一个随机模型。从文档:更新 belongsTo 关系时,可以使用 associate 方法。此方法将在子模型上设置外键:$account = App\Account::find(10);$user->account()->associate($account);$user->save();更新在您的模型工厂中:/** @var Factory $factory */$factory->define(Post::class, function (Faker\Generator $faker) {    return [        // not sure why you do this, is it not a autoincrement column?        'id' => $faker->unique()->randomNumber(3),         'name' => $faker->realText($maxNbChars = 200, $indexSize = 2),        'category_id' => function () {            if ($category = Category::inRandomOrder()->first()) {                return $category->id;            }            return factory(Category::class)->create()->id;        },        'user_id' => function () {            if ($user = User::inRandomOrder()->first()) {                return $user->id;            }            return factory(User::class)->create()->id;        },    ];});在你的播种机中:public function seed(){    factory(User::class, 10)->create();    factory(Category::class, 10)->create();    factory(Post::class, 100)->create();}
打开App,查看更多内容
随时随地看视频慕课网APP