Skip to content

Commit 0936223

Browse files
committed
bulk delete completed
1 parent fca02d1 commit 0936223

File tree

5 files changed

+169
-8
lines changed

5 files changed

+169
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Backend;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Carbon;
7+
use Illuminate\Support\Facades\DB;
8+
use App\Http\Controllers\Controller;
9+
use Illuminate\Database\Query\Builder;
10+
11+
class BulkDeleteController extends Controller
12+
{
13+
public function __invoke(Request $request)
14+
{
15+
$request->validate([
16+
'bulk_delete_from' => ['required', 'date'],
17+
'bulk_delete_to' => ['required', 'date'],
18+
'table' => ['required', 'string'],
19+
]);
20+
$dates = [
21+
Carbon::parse($request->bulk_delete_from)->toDateTimeString(),
22+
Carbon::parse($request->bulk_delete_to)->toDateTimeString(),
23+
];
24+
try {
25+
DB::table($request->table)->where(function (Builder $builder) use ($dates) {
26+
$builder->whereBetween('created_at', $dates);
27+
})->delete();
28+
$notification = [
29+
'alert-type' => 'success',
30+
'message' => 'Bulk delete successful!'
31+
];
32+
} catch (\Throwable $th) {
33+
$notification = [
34+
'alert-type' => 'error',
35+
'message' => 'Something went wrong!'
36+
];
37+
}
38+
39+
40+
return back()->with($notification);
41+
42+
}
43+
}

resources/views/backend/calendar/viewEvents.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
name="{{ empty(request()->query('status')) ? 'All' : str(request()->query('status'))->title()->toString() }} Events">
2323
@can('create event')
2424
<x-backend.ui.button type="custom" href="{{ route('calendar.create') }}"
25-
class="mb-3 btn-sm btn-success">Create</x-backend.ui.button>
25+
class="mb-2 btn-sm btn-success">Create</x-backend.ui.button>
2626
@endcan
2727
<x-backend.table.basic :items="$events">
2828
<thead>

resources/views/backend/layouts/alert.blade.php

+55-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
const Toast = Swal.mixin({
2+
window.Toast = Swal.mixin({
33
toast: true,
44
position: 'top-end',
55
showConfirmButton: false,
@@ -14,7 +14,60 @@
1414
.resumeTimer)
1515
}
1616
})
17-
17+
18+
window.BulkDelete = Swal.mixin({
19+
html: `
20+
<div class="fw-bold fs-3">You are about delete items datewise?</div>
21+
<div class="fw-medium fs-6">This action will delete all items from table by the creation date.</div>
22+
`,
23+
icon: "warning",
24+
buttonsStyling: false,
25+
showCancelButton: true,
26+
confirmButtonText: "Delete Anyway",
27+
cancelButtonText: 'Cancel',
28+
reverseButtons: true,
29+
customClass: {
30+
confirmButton: "btn btn-primary",
31+
cancelButton: 'btn btn-danger'
32+
}
33+
});
34+
window.SuccessAlert = Swal.mixin({
35+
icon: "success",
36+
title: "Success",
37+
text: "Deleted Successfully",
38+
buttonsStyling: false,
39+
showConfirmButton: false,
40+
confirmButtonText: "Ok, Close",
41+
reverseButtons: true,
42+
customClass: {
43+
confirmButton: "btn btn-success",
44+
}
45+
})
46+
window.ErrorAlert = Swal.mixin({
47+
icon: "error",
48+
title: "Oops",
49+
text: "Something went wrong",
50+
buttonsStyling: false,
51+
confirmButtonText: "Ok, Close",
52+
reverseButtons: true,
53+
customClass: {
54+
confirmButton: "btn btn-danger",
55+
}
56+
})
57+
window.LoaderAlert = Swal.mixin({
58+
html: `
59+
<div class="d-flex flex-column gap-3 p-10 align-items-center">
60+
<div class="text-primary d-flex justify-content-center">
61+
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24"><g><circle cx="12" cy="3" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate0" attributeName="r" begin="0;svgSpinners12DotsScaleRotate2.end-0.5s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="16.5" cy="4.21" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate1" attributeName="r" begin="svgSpinners12DotsScaleRotate0.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="7.5" cy="4.21" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate2" attributeName="r" begin="svgSpinners12DotsScaleRotate4.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="19.79" cy="7.5" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate3" attributeName="r" begin="svgSpinners12DotsScaleRotate1.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="4.21" cy="7.5" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate4" attributeName="r" begin="svgSpinners12DotsScaleRotate6.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="21" cy="12" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate5" attributeName="r" begin="svgSpinners12DotsScaleRotate3.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="3" cy="12" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate6" attributeName="r" begin="svgSpinners12DotsScaleRotate8.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="19.79" cy="16.5" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate7" attributeName="r" begin="svgSpinners12DotsScaleRotate5.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="4.21" cy="16.5" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate8" attributeName="r" begin="svgSpinners12DotsScaleRotatea.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="16.5" cy="19.79" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotate9" attributeName="r" begin="svgSpinners12DotsScaleRotate7.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="7.5" cy="19.79" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotatea" attributeName="r" begin="svgSpinners12DotsScaleRotateb.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><circle cx="12" cy="21" r="1" fill="currentColor"><animate id="svgSpinners12DotsScaleRotateb" attributeName="r" begin="svgSpinners12DotsScaleRotate9.begin+0.1s" calcMode="spline" dur="0.6s" keySplines=".27,.42,.37,.99;.53,0,.61,.73" values="1;2;1"/></circle><animateTransform attributeName="transform" dur="6s" repeatCount="indefinite" type="rotate" values="360 12 12;0 12 12"/></g></svg>
62+
</div>
63+
64+
<span>Loading</span>
65+
66+
</div>
67+
`,
68+
buttonsStyling: false,
69+
showConfirmButton: false,
70+
})
1871
1972
$(document).ready(function() {
2073
if ("{{ request()->session()->has('message') }}") {

resources/views/components/backend/table/basic.blade.php

+65-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,56 @@
2525
}
2626
</style>
2727
@endPushOnce
28+
@hasrole('super admin')
29+
@if ($items->first())
30+
<x-backend.ui.button type="button" data-bs-toggle="modal" data-bs-target="#modal-for-bulk-delete"
31+
class="btn-primary btn-sm mb-2 ms-1">
32+
Bulk Delete
33+
</x-backend.ui.button>
34+
<div class="modal fade" id="modal-for-bulk-delete" tabindex="-1" aria-labelledby="modal-for-bulk-delete"
35+
aria-hidden="true">
36+
<div class="modal-dialog modal-dialog-centered">
37+
<div class="modal-content">
38+
39+
<div class="modal-header">
40+
<h5 class="modal-title" id="modal-for-csv">Delete items by date</h5>
41+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
42+
</div>
43+
<form id="bulk-delete-form" action="{{ route('bulk.delete') }}" method="POST">
44+
@csrf
45+
@method('DELETE')
46+
@php
47+
$row = $items->first();
48+
$className = get_class($row);
49+
if ($className != 'stdClass') {
50+
$table = str($className)->explode('\\')->pop();
51+
$table = str($table)->snake()->plural()->toString();
52+
} elseif ($row->notifiable_id) {
53+
$table = 'notifications';
54+
}
55+
@endphp
56+
<input type="hidden" name="table" value="{{ $table }}">
57+
<div class="modal-body">
58+
<div class="d-flex gap-3 align-items-center justify-content-center">
59+
<x-backend.form.text-input required type="date" label="From"
60+
name="bulk_delete_from"></x-backend.form.text-input>
61+
<x-backend.form.text-input required type="date" label="To"
62+
name="bulk_delete_to"></x-backend.form.text-input>
63+
</div>
64+
</div>
65+
<div class="modal-footer">
66+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
67+
<button class="btn btn-primary" type="button" id="bulk-delete-btn">Delete</button>
68+
</div>
69+
</form>
70+
71+
</div>
72+
</div>
73+
</div>
74+
@endif
75+
@endhasrole
2876
<table id="{{ $id }}" class="table table-striped dt-responsive nowrap w-100"
29-
data-page-length='{{gettype($items) != 'string' ? count($items) : 1 }}' data-length-change='false'
77+
data-page-length='{{ gettype($items) != 'string' ? count($items) : 1 }}' data-length-change='false'
3078
data-search="false">
3179
{{ $slot }}
3280
</table>
@@ -61,4 +109,20 @@
61109
});
62110
</script>
63111
<!-- datatable js ends -->
112+
113+
@if ($items->first())
114+
<!-- Bulk delete -->
115+
<script>
116+
$(function() {
117+
$('#bulk-delete-btn').on('click', () => {
118+
BulkDelete.fire()
119+
.then((result) => {
120+
if (result.isConfirmed) {
121+
document.querySelector('#bulk-delete-form').submit()
122+
}
123+
});
124+
})
125+
})
126+
</script>
127+
@endif
64128
@endPushOnce

routes/backend.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
use App\Http\Controllers\Backend\Book\BookCategoryController;
4141
use App\Http\Controllers\Backend\Calendar\CalendarController;
4242
use App\Http\Controllers\Backend\CkEditor\CkEditorController;
43-
use App\Http\Controllers\Backend\Training\TrainingController;
4443
use App\Http\Controllers\Backend\Invoice\InvoiceItemController;
4544
use App\Http\Controllers\Backend\PromoCode\PromoCodeController;
4645
use App\Http\Controllers\Backend\ReturnForm\ReturnFormController;
4746
use App\Http\Controllers\Backend\Service\CustomServiceController;
4847
use App\Http\Controllers\Backend\Withdrawal\WithdrawalController;
4948
use App\Http\Controllers\Backend\Appointment\AppointmentController;
49+
use App\Http\Controllers\Backend\BulkDeleteController;
5050
use App\Http\Controllers\Backend\Product\ProductCategoryController;
5151
use App\Http\Controllers\Backend\Testimonial\TestimonialController;
5252
use App\Http\Controllers\Backend\TaxCalculator\TaxSettingController;
@@ -74,6 +74,7 @@
7474
Route::prefix('admin')
7575
->middleware(['auth', 'can:visit admin panel'])
7676
->group(function () {
77+
Route::match(['delete'], 'bulk-delete-resources', BulkDeleteController::class)->name('bulk.delete')->middleware('role:super admin');
7778

7879
// test
7980
Route::get('test', function () {
@@ -178,9 +179,9 @@
178179
Route::resource('promo-code', PromoCodeController::class)
179180
->except('index')
180181
->middleware('can:manage promo code');
181-
Route::resource('notification', NotificationController::class)
182-
->only('index')
183-
->middleware('can:read promo code');
182+
Route::resource('notification', NotificationController::class)
183+
->only('index')
184+
->middleware('can:read promo code');
184185
Route::resource('notification', NotificationController::class)
185186
->except('index')
186187
->middleware('can:manage promo code');

0 commit comments

Comments
 (0)