laravelnested-sets

laravel 5.7 nested sets


I tried to seed nested set with array with Laravel 5.7 and https://github.com/lazychaser/laravel-nestedset package but always getting :

Array to string conversion error when function gets to main node children. I am testing on example provided on github and cannot make it create tree with nested nodes from array when seeding.

$node = Category::create([
    'name' => 'Foo',

    'children' => [
        [
            'name' => 'Bar',

            'children' => [
                [ 'name' => 'Baz' ],
            ],
        ],
    ],
]);

Can anyone suggest how can I seed the database table or some other package like baum which is working with laravel 5.7 ?

Thank you !


Solution

  • I succeed to make this work, so for laravel 5.7 here are the steps :

    1. create new laravel project, set db params in .env : composer create-project laravel/laravel nestedset

    2. from nested set run : composer require kalnoy/nestedset

    3. run : php artisan make:model NestedSetModel -m

    4. change app/NestedSetModel.php code to:

      namespace App;
      use Kalnoy\Nestedset\NodeTrait;
      
      use Illuminate\Database\Eloquent\Model;
      
      class NestedSetModel extends Model
      {
          use NodeTrait;
      }
      
    5. change database/migrations/xxxx_xx_xx_xxxxxx_create_nested_set_models_table.php to:

      use Illuminate\Support\Facades\Schema;
      use Illuminate\Database\Schema\Blueprint;
      use Illuminate\Database\Migrations\Migration;
      
      class CreateNestedSetModelsTable extends Migration
      {
          /**
           * Run the migrations.
           *
           * @return void
           */
          public function up()
          {
              Schema::create('nested_set_models', function (Blueprint $table) {
                  $table->increments('id');
                  $table->string('name');
                  $table->nestedSet();
                  $table->timestamps();
              });
          }
      
          /**
           * Reverse the migrations.
           *
           * @return void
           */
          public function down()
          {
              Schema::dropIfExists('nested_set_models');
          }
      }
      
    6. run : php artisan make:seeder NestedSetTableSeeder

    7. change database/seeds/NestedSetTableSeeder.php to

      <?php
      
      use Illuminate\Database\Seeder;
      
      class NestedSetTableSeeder extends Seeder
      {
      
      /**
       * Run the database seeds.
       *
       * @return void
       */
      public function run()
      {
          $node = App\NestedSetModel::create([
                  'name' => 'Foo',
                  'children' => [
                      [
                          'name' => 'Bar',
                          'children' => [
                              ['name' => 'Baz'],
                          ],
                      ],
                  ],
          ]);
        }
      }
      
    8. run : php artisan migrate

    9. run : php artisan db:seed

    You should be able to see new table in your database properly seeded.

    1   Foo 1   6       2018-12-03 16:54:20 2018-12-03 16:54:20
    2   Bar 2   5   1   2018-12-03 16:54:20 2018-12-03 16:54:20
    3   Baz 3   4   2   2018-12-03 16:54:20 2018-12-03 16:54:20