Laravel:如何为baum&# 39;的嵌套集播种


Laravel: how to seed Baum's nested set?

我想播种我的一个表,它是一个嵌套集。我正在使用Baum的嵌套集,它提供了一种查看嵌套集的简单方法:https://github.com/etrepat/baum#seeding

我如何使用来自/database/seeds/的Baum ?根据Laravel文档,我需要扩展Seeder类以使种子工作。但是,为了使用Baum的方法,我需要扩展Baum'Node

建议吗?

哦,如果您可以直接使用Baum模型,并且它符合您的目的,我建议使用它。这样你就可以很容易地访问鲍姆的函数。我直接在种子中使用Baum模型,没有扩展种子类。下面是一小段代码:

class BusinessUnitTableSeeder extends Seeder
    {
        public function run()
        {
            $admin = DB::table('user')->where('login_name', '=', 'superadmin')->pluck('id');
            $root = BusinessUnit::create([
                    'name' => 'Headquarters',
                    'created_by' => $admin,
                    'updated_by' => $admin,
                    'owned_by'   => $admin
                ]);
            $user = 'User::find($admin);
            $user->business_unit = $root->id;
            $user->save();
            $child1 = $root->children()->create([
                    'name' => 'Business unit 1',
                    'created_by' => $admin,
                    'updated_by' => $admin,
                    'owned_by'   => $admin
            ]);
            $child2 = $child1->children()->create([
                    'name' => 'Business unit 2',
                    'created_by' => $admin,
                    'updated_by' => $admin,
                    'owned_by'   => $admin
            ]);

        }
    }

UPD:要了解在哪里放置种子以及如何调用主题,请直接参考这里的文档。制作模型非常快。所以我建议你这样做,这样做会减少进一步的行动。模型必须放在models目录下,扩展Baum的模型的快速片段将是

class YourModel extends extends Baum'Node
{
 /**
   * Column name to store the reference to parent's node.
   *
   * @var string
   */
  protected $parentColumn = 'parent_id';
  /**
   * Column name for left index.
   *
   * @var string
   */
  protected $leftColumn = 'lft';
  /**
   * Column name for right index.
   *
   * @var string
   */
  protected $rightColumn = 'rgt';
  /**
   * Column name for depth field.
   *
   * @var string
   */
  protected $depthColumn = 'depth';
/**
         * Table name.
         *
         * @var string
         */
        protected $table = 'your_baum_table';
}

然后直接在你的种子中使用。就这些。此外,我更喜欢的方式添加种子到应用程序是使用migrations:

  1. 创建种子
  2. 创建迁移
  3. 功能up()只需添加Artisan::call('db:seed',['--class'=> 'YourDesiredSeed'])

这样我可以添加演示或实际数据已经工作的应用程序

用于我的产品

我只是用一个数组和baum中使用的变量来做。

我创建了一个模型:

php artisan baum:install Product

这将创建一个模型(我更改为app文件夹)并迁移。只需更新迁移文件:

<?php
use Illuminate'Database'Schema'Blueprint;
use Illuminate'Database'Migrations'Migration;
class CreateProductsTable extends Migration {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::create('products', function(Blueprint $table) {
      // These columns are needed for Baum's Nested Set implementation to work.
      // Column names may be changed, but they *must* all exist and be modified
      // in the model.
      // Take a look at the model scaffold comments for details.
      // We add indexes on parent_id, lft, rgt columns by default.
      $table->increments('id');
      $table->integer('parent_id')->nullable()->index();
      $table->integer('lft')->nullable()->index();
      $table->integer('rgt')->nullable()->index();
      $table->integer('depth')->nullable();
      // Add needed columns here (f.ex: name, slug, path, etc.)
      // $table->string('name', 255);
      $table->string('name',500);
      $table->string('slug');
      $table->string('brand',255);
      $table->string('sku',255)->unique();
      $table->string('description',1000);
      $table->integer('quantity');
      $table->decimal('price',6,2);
      $table->boolean('badge',2);
      $table->string('image1',255);
      $table->boolean('visible',2);
      $table->string('created_at');
      $table->string('updated_at');
      });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::drop('products');
  }
}

然后做你的种子,例如使用。

use App'Product;
use Baum'Node;
use Illuminate'Database'Seeder;
class ProductTableSeeder extends Seeder
{
   public function run()
    {
        $data = array(
            [
                'name'          => 'product1',
                'slug'          => 'slug blablabla',
                'brand'         => 'brand',
                'sku'           => 'xxxxxx',
                'description'   => 'Lorem ipsum dolor sit amet',
                'quantity'      => 100,
                'price'         => 275.00,
                'badge'         => 1,
                'image1'        => 'xxx.jpeg',
                'visible'       => 1,
                'created_at'    => new DateTime,
                'updated_at'    => new DateTime,
            ],
        );
        Product::insert($data);
    }
}