Laravel:用Faker播种嵌套的设置表 [英] Laravel: seeding a nested set table with Faker

查看:53
本文介绍了Laravel:用Faker播种嵌套的设置表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Kalnoy/Nestedset,并尝试使用fakerr为我的注释表添加种子,但是会出现数组到字符串转换"错误.

I'm using Kalnoy/Nestedset and trying to seed my comments table using faker, but get "Array to string conversion" error.

评论表如下:

Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('post_id');
            $table->text('body');
            $table->timestamps();
            $table->nestedSet();
        });

评论工厂:

use Faker\Generator as Faker;

$factory->define(
    App\Models\Comment::class,
    function (Faker $faker) {
        return [
        'user_id' => function () {
            return factory('App\Models\User')->create()->id;
        },
        'post_id' => function () {
            return factory('App\Models\Post')->create()->id;
        },
        'body' => $faker->paragraph,
        ];
    }
); 

我真的无法弄清楚播种机的外观.这是我的尝试:

And I can't really figure out what the seeder must look like. Here's my try:

public function run(Post $post)
    {
        $node = factory('App\Models\Comment'::class, 3)->create([

            'children' => [
                [
                    factory('App\Models\Comment'::class, 2)->create([
                        'post_id' => $post->id
                    ]),

                    'children' => [
                        [ factory('App\Models\Comment'::class, 1)->create([
                        'post_id' => $post->id
                            ]), 
                        ],
                    ],
                ],
            ],
        ]);
    }

}

我还想确保子代的帖子ID与父代的ID相同,但现在它返回null.

I also want to make sure that the post id of children as the same as parent's, but now it returns null.

推荐答案

create 方法中数组的键应为模型上存在的属性.在您的情况下,子项不是 Comment 模型的属性.

The keys of the array in the create method should be attributes that exist on your model. In your case children isn't an attribute on the Comment model.

使用使用工厂文档中的示例,您可以而是创建每个注释,然后在新模型上使用 children()关系创建其子级.例如:

Using the example from the Using Factories documentation, you could rather create each comment and then use the children() relationship on the new models to create their children. For example:

public function run(Post $post)
{
    $node = factory('App\Models\Comment'::class, 3) // Create the root comments.
            ->create()
            ->each(function ($comment) use ($post) { // Add children to every root.
                $comment->children()->saveMany(factory(App\Comment::class, 2)->make([
                    'post_id' => $post->id
                ]))
                ->each(function ($comment)  use ($post) { // Add children to every child of every root.
                     $comment->children()->saveMany(factory(App\Comment::class, 2)->make([
                        'post_id' => $post->id
                    ]));
                });
            });

这篇关于Laravel:用Faker播种嵌套的设置表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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