mercredi 27 mars 2019

laravel-nestedset children relation not populated on toTree()

I'm using laravel-nestedset with Laravel 5.7.

When using the toTree() method, the children are not included in the results.

My table is created like:

        Schema::connection('tenant')->create('menus', function (Blueprint $table) {
            $table->bigIncrements('id')->comment('A unique Id to identify this row.')->unsigned();
            $table->string('name', 255)->comment('A unique display name for this menu')->unique();
            $table->text('description')->comment('An optional description for this menu')->nullable();
            $table->string('color', 50)->comment('A hex color code for this menu')->nullable();
            $table->string('icon', 100)->comment('An optional icon for this menu')->nullable();
            $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
            $table->softDeletes();
            $table->nestedSet();
        });

My Menu model is defined as:

class Menu extends TenantModel
{

    use \Illuminate\Database\Eloquent\SoftDeletes,
        \Kalnoy\Nestedset\NodeTrait;

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'description', 'color', 'icon', 'updated_at', 'created_at', 'parent_id'];
}

I'm creating a menu tree like:

        Menu::create([
            'name' => 'Root Menu',
            'description' => 'This is the default main menu for the application',
            'children' => [
                [
                    'name' => 'Test Level 1',
                    'description' => 'This a top level application menu.',
                    'children' => [
                        [
                            'name' => 'Test Level 2',
                            'description' => 'This a nested level application menu.',
                        ],
                    ],
                ],
            ],
        ]);

This creates the rows I expect:

enter image description here

However, if I get the root menu item like below, the "children" relation is not filled:

>>> $nodes = Menu::where('name','=', 'Root Menu')->get()->toTree();
=> Kalnoy\Nestedset\Collection {#3161
     all: [
       Menu {#3176
         id: 1,
         name: "Root Menu",
         description: "This is the default main menu for the application",
         color: null,
         icon: null,
         created_at: "2019-03-28 00:23:31",
         updated_at: "2019-03-27 19:23:32",
         deleted_at: null,
         _lft: 1,
         _rgt: 6,
         parent_id: null,
         parent: null,
         children: Illuminate\Database\Eloquent\Collection {#3188
           all: [],
         },
       },
     ],
   }

For some reason, children is an empty collection:

>>> $nodes[0]->children
=> Illuminate\Database\Eloquent\Collection {#3188
     all: [],
}

Which is odd because if I manually load the relations as below, it seems to work properly:

>>> $nodes = Menu::where('name','=', 'Root Menu')->with('children.children')->get()
=> Kalnoy\Nestedset\Collection {#3207
     all: [
       Menu {#3214
         id: 1,
         name: "Root Menu",
         description: "This is the default main menu for the application",
         color: null,
         icon: null,
         created_at: "2019-03-28 00:23:31",
         updated_at: "2019-03-27 19:23:32",
         deleted_at: null,
         _lft: 1,
         _rgt: 6,
         parent_id: null,
         children: Kalnoy\Nestedset\Collection {#3215
           all: [
             Menu {#3228
               id: 2,
               name: "Test Level 1",
               description: "This a top level application menu.",
               color: null,
               icon: null,
               created_at: "2019-03-28 00:23:31",
               updated_at: "2019-03-27 19:23:32",
               deleted_at: null,
               _lft: 2,
               _rgt: 5,
               parent_id: 1,
               children: Kalnoy\Nestedset\Collection {#3231
                 all: [
                   Menu {#3242
                     id: 3,
                     name: "Test Level 2",
                     description: "This a nested level application menu.",
                     color: null,
                     icon: null,
                     created_at: "2019-03-28 00:23:31",
                     updated_at: "2019-03-28 00:23:31",
                     deleted_at: null,
                     _lft: 3,
                     _rgt: 4,
                     parent_id: 2,
                   },
                 ],
               },
             },
           ],
         },
       },
     ],
   }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire