laravel5.1で、カテゴリー・サブカテゴリーのような親子セレクトボックスを作る方法
laravel5.1で、カテゴリー・サブカテゴリーのような親子セレクトボックスを作る方法 1, 親カテゴリー・サブカテゴリーのテーブルを作る。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// 親カテゴリー public function up() { Schema::create('product_categories', function (Blueprint $table) { $table->increments('id')->comment('主キー'); $table->string('category_name')->comment('カテゴリー名'); $table->integer('orderby_id')->comment('表示順序 管理画面で並び替えられる'); $table->timestamps(); $table->softDeletes(); }); // スキーマビルダーではテーブル自体のコメントは出来ない?のでSQL文で実行 DB::statement("ALTER TABLE ".DB::getTablePrefix()."product_categories COMMENT '商品カテゴリ'"); } // サブカテゴリー public function up() { Schema::create('product_subcategories', function (Blueprint $table) { $table->increments('id')->comment('主キー'); $table->integer('parent_category_id')->unsigned()->comment('親カテゴリーID 子カテゴリーの場合は親id'); $table->string('subcategory_name')->comment('カテゴリー名 '); $table->integer('orderby_id')->comment('表示順序 管理画面で並び替えられる'); $table->timestamps(); $table->softDeletes(); // 親カテゴリーIDは、必ずproduct_categoriesに存在する事! $table->foreign('parent_category_id')->references('id')->on('product_categories'); }); } |