Skip to content

Commit

Permalink
Admin Category Delete: complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Slymee committed Dec 28, 2023
1 parent 87c5dcf commit 5cc273a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
9 changes: 7 additions & 2 deletions app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ public function update(Request $request)
/**
* Remove the specified resource from storage.
*/
public function destroy(Category $categoryAndSubCategory)
public function destroy($category_id)
{

try{
Category::find($category_id)->delete();
return redirect()->back()->with('message', 'Category Deleted');
}catch(\Exception $e){
return redirect()->back()->with('message', $e->getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public function up(): void
$table->string('category_name');
$table->integer('parent_id')->nullable();
$table->timestamps();
$table->foreign('parent_id')->references('id')->on('categories')->onDelete('cascade')->onUpdate('cascade');
});

Schema::table('categories', function (Blueprint $table) {
$table->foreign('parent_id')->references('id')->on('categories')->onDelete('cascade');
});
// Schema::table('categories', function (Blueprint $table) {
// $table->foreign('parent_id')->references('id')->on('categories')->onDelete('cascade')->onUpdate('cascade');
// });
}

/**
Expand Down
15 changes: 14 additions & 1 deletion resources/views/backend/adminCategory.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<div class="display-data-container">
<span>Categories</span>


<div class="table-container">
<table>
<tr>
Expand All @@ -33,11 +34,23 @@
<td>{{ $data->category_name }}</td>
<td><button>Sub-categories</button></td>
<td><a href={{ route('admin.edit.category.form', $data->id) }}><button>Edit</button></a></td>
<td><button>Delete</button></td>
<td><button onclick="confirmDelete({{ $data->id }})">Delete</button></td>
</tr>
@endforeach

</table>
</div>


</div>

<script>
function confirmDelete(categoryID){
var result = window.confirm('Are you sure you want to delete this category');
if(result){
window.location.href = '/admin-delete-category/'+ categoryID;
}
}
</script>
@endsection
2 changes: 1 addition & 1 deletion resources/views/backend/modals/adminAddCategory.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<option value="">-- Select Parent Category --</option>
@if($datas)
@foreach ($datas as $data)
<option value={{ $data->id }} {{ }}>{{ $data->category_name }}</option>
<option value={{ $data->id }}>{{ $data->category_name }}</option>
@endforeach
@endif
</select>
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
Route::post('/admin-category-add/insert', [CategoryController::class, 'insertCategory'])->middleware('auth')->name('admin.insert.category');
Route::get('/admin-category-edit/{category_id}', [CategoryController::class, 'edit'])->middleware('auth')->name('admin.edit.category.form');
Route::post('/admin-category-edit/update', [CategoryController::class, 'update'])->middleware('auth')->name('admin.edit.category');
Route::get('/admin-delete-category/{category_id}', [CategoryController::class, 'destroy'])->middleware('auth')->name('admin.delete.category');


//Forgot Password Routess
Expand Down

0 comments on commit 5cc273a

Please sign in to comment.