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 !
I succeed to make this work, so for laravel 5.7 here are the steps :
create new laravel project, set db params in .env : composer create-project laravel/laravel nestedset
from nested set run : composer require kalnoy/nestedset
run : php artisan make:model NestedSetModel -m
change app/NestedSetModel.php code to:
namespace App;
use Kalnoy\Nestedset\NodeTrait;
use Illuminate\Database\Eloquent\Model;
class NestedSetModel extends Model
{
use NodeTrait;
}
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');
}
}
run : php artisan make:seeder NestedSetTableSeeder
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'],
],
],
],
]);
}
}
run : php artisan migrate
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