diff --git a/app/Http/Controllers/CategoryAndSubCategoryController.php b/app/Http/Controllers/CategoryAndSubCategoryController.php index 91d9279..303c71b 100644 --- a/app/Http/Controllers/CategoryAndSubCategoryController.php +++ b/app/Http/Controllers/CategoryAndSubCategoryController.php @@ -17,15 +17,31 @@ public function index() //Add Category Form public function addCategoryFormDisplay(){ - return view('modals.adminAddCategory'); + $datas = CategoryAndSubCategory::all(); + return view('modals.adminAddCategory', ['datas' => $datas]); } /** - * Store a newly created resource in storage. + * Insert new category. */ - public function store(Request $request) + public function insertCategory(Request $request) { - // + try{ + $request->validate([ + 'category_name' => ['bail', 'required'], + 'parent_id' => ['nullable', 'exists:category_and_sub_categories,id'], + ]); + + + CategoryAndSubCategory::create([ + 'category_name' => $request->category_name, + 'parent_id' => $request->parent_id, + ]); + + return redirect()->back()->with('message', 'Insert Success.'); + }catch(\Exception $e){ + return redirect()->back()->with('message', $e->getMessage()); + } } /** diff --git a/app/Models/CategoryAndSubCategory.php b/app/Models/CategoryAndSubCategory.php index 779d35b..def4580 100644 --- a/app/Models/CategoryAndSubCategory.php +++ b/app/Models/CategoryAndSubCategory.php @@ -8,4 +8,20 @@ class CategoryAndSubCategory extends Model { use HasFactory; + + protected $table = 'category_and_sub_categories'; + + protected $fillable = [ + 'category_name', + 'parent_id', + ]; + + + public function children(){ + return $this->hasMany(CategoryAndSubCategory::class, 'parent_id', 'id'); + } + + public function parent(){ + return $this->belongsTo(CategoryAndSubCategory::class, 'parent_id'); + } } diff --git a/resources/css/admin-dashboard-category.css b/resources/css/admin-dashboard-category.css index 02770d1..5d4cfc8 100644 --- a/resources/css/admin-dashboard-category.css +++ b/resources/css/admin-dashboard-category.css @@ -49,7 +49,7 @@ .form-container{ padding: 10px; width: 40%; - height: 30%; + height: 37%; background: white; position: absolute; top: 50%; @@ -92,3 +92,18 @@ input[type=text]:focus{ ::placeholder{ color: rgb(61,81,181); } + + + +.message{ + color: red; + font-size: 18px; +} + +select{ + padding: 10px; + width: 100%; + font-size: 18px; + margin-top: 10px; + color: rgb(61,81,181); +} \ No newline at end of file diff --git a/resources/views/backend/adminCategory.blade.php b/resources/views/backend/adminCategory.blade.php index 2b43a2e..3c2ad89 100644 --- a/resources/views/backend/adminCategory.blade.php +++ b/resources/views/backend/adminCategory.blade.php @@ -29,12 +29,13 @@