diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index 3dec8d1..e365315 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -17,6 +17,14 @@ public function index() } //Add Category Form + + /** + * @Aashish + * + * instead of wrting custom function like this, try to use laravel resource controller defined functions + * resource controller provides predefiend functions like "index, store, update, edit, destroy" + * + */ public function addCategoryFormDisplay(){ $datas = Category::whereNull('parent_id') ->orWhereHas('parent', fn ($query) => $query->whereNull('parent_id')) @@ -31,6 +39,11 @@ public function addCategoryFormDisplay(){ public function insertCategory(Request $request) { try{ + /** + * @Aashish + * + * Use form request here. Use controller to handle request only. + */ $request->validate([ 'category_name' => ['bail', 'required'], 'parent_id' => ['nullable', 'exists:categories,id'], @@ -51,9 +64,18 @@ public function insertCategory(Request $request) /** * Display the specified resource. */ + public function subCategoryIndex($id) { $parentCategory = Category::find($id); + /** + * @Aashish + * in sub category view if you want to display child as well + * instead of loading child and passing variable try to use relationship in the view file + * + * Remove this below code + * + */ $immediateChildren = $parentCategory->children; $subSubCategories = []; @@ -84,6 +106,12 @@ public function edit($category_id) public function update(Request $request) { try{ + /** + * @Aashish + * + * Use form request + * remove below validation code + */ $request->validate([ 'category_name' => ['bail', 'required'], 'category_id' => ['required'], @@ -91,8 +119,21 @@ public function update(Request $request) ]); - + /** + * @Aashish + * + * Instead of quering or find here use route model binding. "Search for route model binding in laravel" + * + */ $category = Category::find($request->input('category_id')); + + /** + * @Aashish + * + * why use update and save both? + * + * either use update or save. + */ $category->update($request->all()); $category->save(); return redirect()->back()->with('message', 'Edit Successful'); @@ -109,6 +150,13 @@ public function update(Request $request) public function destroy($category_id) { try{ + /** + * @Aashish + * + * RecursiveDelete deletes its related childs + * + * but remove this instead try to delete using relationship defined in model + */ Category::find($category_id)->recursiveDelete(); return redirect()->back()->with('message', 'Category Deleted'); }catch(\Exception $e){ diff --git a/routes/web.php b/routes/web.php index 76b48c4..b198b78 100644 --- a/routes/web.php +++ b/routes/web.php @@ -33,6 +33,13 @@ //admin dashboard Route::get('/dashboard', [DashboardController::class, 'index'])->middleware('auth')->name('admin.dashboard'); + +/** + * Instead of writing middleware auth in every routes, Group the route. "Search Route Grouping" + * + * For CRUD operation instead of writing individual routes, use laravel ResourceController " Search ResourceController in laravel " + * + */ Route::get('/admin-category', [CategoryController::class, 'index'])->middleware('auth')->name('category.and.subcategory'); Route::get('/admin-category/subcategory/{id}', [CategoryController::class, 'subCategoryIndex'])->middleware('auth')->name('admin.subcategory'); Route::get('/admin-category-add', [CategoryController::class, 'addCategoryFormDisplay'])->middleware('auth')->name('add.category.form');