Reputation: 40633
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?
Upvotes: 2
Views: 2809
Reputation: 21
I used for my products
Just I do it using an array with the variables used in baum.
I created a model using:
php artisan baum:install Product
this will create a model (I changed to app folder)and migration. Just update migration file:
<?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');
}
}
Then just do your seed for using for example.
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);
}
}
Upvotes: 0
Reputation: 5755
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:
up()
just add Artisan::call('db:seed',['--class'=> 'YourDesiredSeed'])
.This way I can add demo or real data on already working apps.
Upvotes: 2