From 37b5d71d87a00cc27b853347e5b0a68f1c86e6ee Mon Sep 17 00:00:00 2001 From: Slymee Date: Thu, 21 Dec 2023 13:15:22 +0545 Subject: [PATCH] Inserting category and sub-category: WIP --- .../CategoryAndSubCategoryController.php | 24 +++++++++++++++---- app/Models/CategoryAndSubCategory.php | 16 +++++++++++++ resources/css/admin-dashboard-category.css | 17 ++++++++++++- .../views/backend/adminCategory.blade.php | 3 ++- .../views/modals/adminAddCategory.blade.php | 22 ++++++++++++++--- routes/web.php | 3 ++- 6 files changed, 75 insertions(+), 10 deletions(-) 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 @@ SN Category Name - Utilities + Utilities 1 Fruit + diff --git a/resources/views/modals/adminAddCategory.blade.php b/resources/views/modals/adminAddCategory.blade.php index d7463f0..e223214 100644 --- a/resources/views/modals/adminAddCategory.blade.php +++ b/resources/views/modals/adminAddCategory.blade.php @@ -20,10 +20,26 @@
Add a Category -
-
-
+ + @csrf +
+
+ + + @if (session('message')) + Category Inserted + @endif +
diff --git a/routes/web.php b/routes/web.php index 4f20fc9..0f45e7d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -35,7 +35,8 @@ //admin dashboard Route::get('/dashboard', [DashboardController::class, 'index'])->middleware('auth')->name('admin.dashboard'); Route::get('/admin-category', [CategoryAndSubCategoryController::class, 'index'])->middleware('auth')->name('category.and.subcategory'); -Route::get('admin-category-add', [CategoryAndSubCategoryController::class, 'addCategoryFormDisplay'])->name('add.category.form'); +Route::get('/admin-category-add', [CategoryAndSubCategoryController::class, 'addCategoryFormDisplay'])->name('add.category.form'); +Route::post('/admin-category-add/insert', [CategoryAndSubCategoryController::class, 'insertCategory'])->name('admin.insert.category'); //Forgot Password Routess