Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
aashish-t-magar committed Dec 29, 2023
1 parent 34819e5 commit ec0c91d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
50 changes: 49 additions & 1 deletion app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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'],
Expand All @@ -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 = [];

Expand Down Expand Up @@ -84,15 +106,34 @@ 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'],
'parent_id' => ['nullable', 'exists:categories,id'],
]);



/**
* @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');
Expand All @@ -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){
Expand Down
7 changes: 7 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit ec0c91d

Please sign in to comment.