Skip to content

Commit 015e7e3

Browse files
Finish user page search functionality
1 parent 430403f commit 015e7e3

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

app/Http/Controllers/UserController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ class UserController extends Controller
1111
/**
1212
* Display a listing of the resource.
1313
*/
14-
public function index()
14+
public function index(Request $request)
1515
{
1616
return Inertia::render('Users/Index', [
17-
'users' => User::with('role')->get()
17+
'users' => User::with('role')->when($request->term, function ($query, $term) {
18+
$query->where('name', 'LIKE', '%' . $term . '%');
19+
})->paginate(5),
1820
]);
1921
}
2022

app/Models/User.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Models;
44

5-
use Illuminate\Contracts\Auth\MustVerifyEmail;
65
use Illuminate\Database\Eloquent\Factories\HasFactory;
76
use Illuminate\Foundation\Auth\User as Authenticatable;
87
use Illuminate\Notifications\Notifiable;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<div class="m-5" v-if="links.length > 3">
3+
<div class="flex flex-wrap -mb-1">
4+
<template v-for="(link, p) in links" :key="p">
5+
<div v-if="link.url === null" class="mr-1 mb-1 px-4 py-3 text-sm leading-4 text-gray-400 border rounded"
6+
v-html="link.label" />
7+
<inertia-link v-else
8+
class="mr-1 mb-1 px-4 py-3 text-sm leading-4 border rounded focus:border-indigo-500 focus:text-indigo-500"
9+
:class="{ 'bg-blue-700 text-white': link.active }" :href="link.url" v-html="link.label" />
10+
</template>
11+
</div>
12+
</div>
13+
</template>
14+
<script>
15+
export default {
16+
props: {
17+
links: Array
18+
},
19+
}
20+
</script>

resources/js/Pages/Users/Index.vue

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
<div class="mb-3 xl:w-96">
2323
<div class="relative flex items-stretch w-4/5 mb-3 input-group">
2424

25-
<input id="search" type='text' v-model="term" @keyup="search"
25+
<input type="text" v-model="term" @keyup="search">
26+
27+
<!-- <input id="search" type='text' v-model="term" @keyup="search"
2628
class="outline-none focus:ring-0 rounded-r-none form-control relative min-w-0 block w-full px-3 py-1.5 text-base font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0"
27-
placeholder="Search...">
29+
placeholder="Search..."> -->
2830

2931
<button
3032
class="rounded-l-none btn px-6 py-2.5 bg-blue-600 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out flex items-center"
@@ -64,7 +66,7 @@
6466
</tr>
6567
</thead>
6668
<tbody class="bg-white divide-y divide-gray-200">
67-
<tr v-for="user in users" :key="user.id">
69+
<tr v-for="user in users.data" :key="user.id">
6870
<td class="px-6 py-4 whitespace-nowrap">
6971
<div class="flex items-center justify-center">
7072
<div class="ml-4">
@@ -113,7 +115,7 @@
113115
</tr>
114116
</tbody>
115117
</table>
116-
<!-- <Pagination class="mt-6" :links="users.links" /> -->
118+
<Pagination class="mt-6" :links="users.links" />
117119
</div>
118120
</div>
119121
</div>
@@ -122,18 +124,28 @@
122124

123125
<script>
124126
import AppLayout from '@/Layouts/AppLayout.vue';
127+
import Pagination from '@/Components/Pagination.vue';
125128
export default {
126129
components: {
127-
AppLayout
130+
AppLayout,
131+
Pagination
128132
},
129133
props: {
130134
users: Object
131135
},
136+
data() {
137+
return {
138+
term: this.$page.term || '',
139+
}
140+
},
132141
methods: {
133142
deleteUser(user) {
134143
if (!confirm('Are you sure you want to delete user?')) return;
135144
this.$inertia.delete(route('users.destroy', user.id));
136-
}
145+
},
146+
search() {
147+
this.$inertia.replace(this.route('users.index', { term: this.term }))
148+
},
137149
}
138150
}
139151
</script>

0 commit comments

Comments
 (0)