I did some research on how to seed with foreign keys, and most of them include faker, which then randomly assigns those fields.
Unfortunately, this doesn't really work for me (I think) as I need the same data every time.
I have a relationship of
category()->subCategory()->subSubCategory()->product
I keep all categories and subcategories in the database so that I could update, rearrange, or delete them.
I want to be able to regenerate them on the fly rather than creating a category then going creating subcategories and later creating subSubCategories which all foreign key constrained.
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
});
Schema::create('sub_categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
$table->foreign('category_id')
->references('id')->on('categories')
->onDelete('cascade');
});
Schema::create('sub_sub_categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('sub_category_id')->unsigned();
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
$table->foreign('sub_category_id')
->references('id')->on('sub_categories')
->onDelete('cascade');
});
So I was thinking of using seeder for this to make my life easier
class CategoryTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//categories
DB::table('categories')->insert([
'name' => 'Clothing',
'slug' => 'clothing',
]);
DB::table('categories')->insert([
'name' => 'Shoes',
'slug' => 'shoes',
]);
//subcategories
DB::table('sub_categories')->insert([
'category_id' => '1',
'name' => 'Womens',
'slug' => 'womens',
]);
DB::table('sub_categories')->insert([
'category_id' => '1',
'name' => 'Mens',
'slug' => 'mens',
]);
}
}
But I face with
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`paciaisau`.`sub_categories`, CONSTRAINT `sub_categories_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE) (SQL: insert into `sub_categories` (`category_id`, `name`, `slug`) values (1, Womens, womens))
What would be the right way of doing this?