Skip to content

Commit 5878c48

Browse files
Finish multi delete users functionality
1 parent ec8f708 commit 5878c48

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ This application includes the following features:
5454
- Browser sessions
5555
- Full text searching
5656
- Pagination
57+
- Multi delete users
5758

5859
## Contributing
5960

app/Http/Controllers/UserController.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ public function destroy(string $id)
7878

7979
return back()->with('delete', 'User has been deleted!');
8080
}
81-
}
81+
82+
public function deleteMultiple(Request $request)
83+
{
84+
$userIds = $request->input('ids');
85+
User::whereIn('id', $userIds)->delete();
86+
87+
return redirect()->route('users.index')->with('success', 'Operation successfully!');
88+
}
89+
}

resources/js/Pages/Users/Index.vue

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818

1919
<div class="mx-auto max-w-7xl sm:px-6 lg:px-8">
2020
<div class="overflow-hidden bg-white shadow-xl sm:rounded-lg">
21+
22+
<a href="#" @click="deleteSelectedUsers"
23+
class="float-left px-4 mt-3 py-2 text-red-400 duration-100 rounded hover:text-red-600">
24+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="h-6 w-6">
25+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" stroke="currentColor"
26+
fill="none"
27+
d="M3 6h18M6 6l1 12a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2l1-12M9 4v-1a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v1" />
28+
</svg>
29+
</a>
30+
2131
<div class="flex justify-end mt-3">
2232
<div class="mb-3 xl:w-96">
2333
<div class="relative flex items-stretch w-4/5 mb-3 input-group">
@@ -39,9 +49,15 @@
3949
</div>
4050
</div>
4151
</div>
52+
4253
<table class="min-w-full divide-y divide-gray-200">
4354
<thead class="bg-gray-50">
4455
<tr>
56+
<th scope="col"></th>
57+
<th scope="col"
58+
class="px-6 py-3 text-xs font-medium tracking-wider text-center text-gray-500 uppercase">
59+
ID
60+
</th>
4561
<th scope="col"
4662
class="px-6 py-3 text-xs font-medium tracking-wider text-center text-gray-500 uppercase">
4763
Name
@@ -65,8 +81,18 @@
6581
</thead>
6682
<tbody class="bg-white divide-y divide-gray-200">
6783
<tr v-for="user in users.data" :key="user.id">
84+
<td>
85+
<input type="checkbox" v-model="selectedUsers" :value="user.id"
86+
class="ml-5 outline-none" />
87+
</td>
88+
<td class="px-6 py-4 whitespace-nowrap">
89+
<div class="text-sm text-center text-gray-900">
90+
{{ user.id }}
91+
</div>
92+
</td>
6893
<td class="px-6 py-4 whitespace-nowrap">
6994
<div class="flex items-center justify-center">
95+
7096
<div class="ml-4">
7197
<div class="text-sm font-medium text-gray-900">
7298
<inertia-link class="transition hover:text-blue-500"
@@ -109,6 +135,7 @@
109135
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
110136
</svg>
111137
</a>
138+
112139
</td>
113140
</tr>
114141
</tbody>
@@ -133,6 +160,7 @@ export default {
133160
},
134161
data() {
135162
return {
163+
selectedUsers: [],
136164
term: this.$page.term || '',
137165
}
138166
},
@@ -150,6 +178,24 @@ export default {
150178
search() {
151179
this.$inertia.replace(this.route('users.index', { term: this.term }))
152180
},
181+
async deleteSelectedUsers() {
182+
if (this.selectedUsers.length === 0) {
183+
return;
184+
}
185+
186+
const confirmed = confirm(`Are you sure you want to delete ${this.selectedUsers.length} user(s)?`);
187+
if (!confirmed) {
188+
return;
189+
}
190+
191+
try {
192+
await this.$inertia.post('/users/delete', { ids: this.selectedUsers });
193+
this.selectedUsers = [];
194+
} catch (error) {
195+
console.error(error);
196+
alert('An error occurred while deleting the selected users.');
197+
}
198+
},
153199
}
154200
}
155201
</script>

routes/web.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
]);
1616
});
1717

18+
Route::post('/users/delete', [UserController::class, 'deleteMultiple'])->name('users.delete');
19+
20+
1821
Route::resource('/users', UserController::class);
1922
Route::resource('/roles', RoleController::class);
2023

0 commit comments

Comments
 (0)