StackOverflowNewbie StackOverflowNewbie - 6 months ago 58
PHP Question

Laravel: how to seed Baum's nested set?

I'd like to seed one of my tables that is a nested set. I'm using Baum's nested set, which provides an easy way to see a nested set: https://github.com/etrepat/baum#seeding

How do I use Baum from

/database/seeds/
? Based on the Laravel documention, I would need to extend the
Seeder
class in order for seeding to work. However, in order to use Baum's methods, I need to extend
Baum\Node
.

Suggestions?

Answer

Oh well, If you can use the Baum model directly and it's ok for your purposes, I suggest to use it. This way you have easy access to baum's functions. I use my Baum model directly in the seed, without extending the seeder class. Here's a little snippet :

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: To find out where to place your seeds and how to call theme, please refer the docs directly here. Making the model is extremely fast. so I would recomend you to do so, it would cause less actions to do further. The model must be placed at the models directory, and the fast snippet of model extending the Baum would be

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';

}

And then use it directly in your seed. That is all . Also, my preferable way of adding seeds to the app is using the migrations:

  1. Create the seed
  2. Create the migration
  3. in function up() just add Artisan::call('db:seed',['--class'=> 'YourDesiredSeed']).

This way I can add demo or real data on already working apps.