laravel 5.7 嵌套集

标签 laravel nested-sets

我尝试使用 Laravel 5.7 和 https://github.com/lazychaser/laravel-nestedset 为嵌套集播种数组包但总是得到:

数组到字符串的转换错误 当函数到达主节点子节点时。 我正在测试 github 上提供的示例,并且无法在播种时使用数组中的嵌套节点创建树。

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

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

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

任何人都可以建议我如何为数据库表或与 laravel 5.7 一起工作的其他包(如 baum)做种子吗?

谢谢!

最佳答案

我成功地完成了这项工作,所以对于 laravel 5.7 以下是步骤:

  1. 创建新的 laravel 项目,在 .env 中设置数据库参数:composer create-project laravel/laravel nestedset

  2. 从嵌套集运行:composer require kalnoy/nestedset

  3. 运行:php artisan make:model NestedSetModel -m

  4. 将 app/NestedSetModel.php 代码更改为:

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

    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. 运行:php artisan make:seeder NestedSetTableSeeder

  7. 将 database/seeds/NestedSetTableSeeder.php 更改为

    <?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. 运行:php artisan migrate

  9. 运行:php artisan db:seed

您应该能够在数据库中看到正确植入的新表。

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

关于laravel 5.7 嵌套集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53594010/

相关文章:

php - Laravel 中的字符串插值

javascript - Laravel 将两个 jquery 函数结合在一个按钮中,将产品插入购物车

php - laravel中如何使用foreach向数据库中添加数据

sql - 计算嵌套集中某个级别中的节点数

mysql - 在 MySQL 中使用嵌套集进行分层产品计数

php date->diff() 返回无意义的金额

Laravel sqlite 相对路径

php - Doctrine2 树菜单的 Symfony2 路由

arrays - 在 matlab 中,我应该使用什么来收集不同对象的集合?