user pagination
This commit is contained in:
@@ -2,7 +2,8 @@
|
|||||||
"permissions": {
|
"permissions": {
|
||||||
"allow": [
|
"allow": [
|
||||||
"Bash(php artisan *)",
|
"Bash(php artisan *)",
|
||||||
"Bash(vendor/bin/pint --dirty --format agent)"
|
"Bash(vendor/bin/pint --dirty --format agent)",
|
||||||
|
"mcp__laravel-boost__search-docs"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,17 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
public function index()
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$users = User::orderBy('name')->get();
|
$users = User::orderBy('name')->paginate(10);
|
||||||
|
|
||||||
|
if ($request->ajax()) {
|
||||||
|
return view('users._table', compact('users'));
|
||||||
|
}
|
||||||
|
|
||||||
return view('users.index', compact('users'));
|
return view('users.index', compact('users'));
|
||||||
}
|
}
|
||||||
|
|||||||
34
resources/views/users/_table.blade.php
Normal file
34
resources/views/users/_table.blade.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<table class="w-full text-sm text-left text-gray-900 dark:text-gray-100">
|
||||||
|
<thead class="text-xs text-gray-700 dark:text-gray-400 uppercase bg-gray-50 dark:bg-gray-700">
|
||||||
|
<tr>
|
||||||
|
<th class="px-6 py-3">#</th>
|
||||||
|
<th class="px-6 py-3">{{ __('Name') }}</th>
|
||||||
|
<th class="px-6 py-3">{{ __('Email') }}</th>
|
||||||
|
<th class="px-6 py-3">{{ __('Joined') }}</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach ($users as $user)
|
||||||
|
<tr class="{{ $loop->even ? 'bg-gray-50 dark:bg-gray-700' : 'bg-white dark:bg-gray-800' }} border-b border-gray-200 dark:border-gray-600">
|
||||||
|
<td class="px-6 py-4">{{ $users->firstItem() + $loop->index }}</td>
|
||||||
|
<td class="px-6 py-4 font-medium">{{ $user->name }}</td>
|
||||||
|
<td class="px-6 py-4">{{ $user->email }}</td>
|
||||||
|
<td class="px-6 py-4">{{ $user->created_at->format('d M Y') }}</td>
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
|
||||||
|
@if ($users->isEmpty())
|
||||||
|
<tr>
|
||||||
|
<td colspan="4" class="px-6 py-4 text-center text-gray-500 dark:text-gray-400">
|
||||||
|
{{ __('No users found.') }}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
@endif
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
@if ($users->hasPages())
|
||||||
|
<div class="mt-4">
|
||||||
|
{{ $users->links() }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
@@ -8,35 +8,33 @@
|
|||||||
<div class="py-12">
|
<div class="py-12">
|
||||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||||
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
|
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
|
||||||
<div class="p-6">
|
<div
|
||||||
<table class="w-full text-sm text-left text-gray-900 dark:text-gray-100">
|
class="p-6"
|
||||||
<thead class="text-xs text-gray-700 dark:text-gray-400 uppercase bg-gray-50 dark:bg-gray-700">
|
x-data="{
|
||||||
<tr>
|
loading: false,
|
||||||
<th class="px-6 py-3">#</th>
|
async paginate(url) {
|
||||||
<th class="px-6 py-3">{{ __('Name') }}</th>
|
this.loading = true;
|
||||||
<th class="px-6 py-3">{{ __('Email') }}</th>
|
const response = await fetch(url, {
|
||||||
<th class="px-6 py-3">{{ __('Joined') }}</th>
|
headers: { 'X-Requested-With': 'XMLHttpRequest' }
|
||||||
</tr>
|
});
|
||||||
</thead>
|
this.$refs.tableContainer.innerHTML = await response.text();
|
||||||
<tbody>
|
this.loading = false;
|
||||||
@foreach ($users as $index => $user)
|
}
|
||||||
<tr class="{{ $index % 2 === 0 ? 'bg-white dark:bg-gray-800' : 'bg-gray-50 dark:bg-gray-700' }} border-b border-gray-200 dark:border-gray-600">
|
}"
|
||||||
<td class="px-6 py-4">{{ $index + 1 }}</td>
|
@click.prevent="
|
||||||
<td class="px-6 py-4 font-medium">{{ $user->name }}</td>
|
const link = $event.target.closest('a[href]');
|
||||||
<td class="px-6 py-4">{{ $user->email }}</td>
|
if (link && $refs.tableContainer.contains(link)) {
|
||||||
<td class="px-6 py-4">{{ $user->created_at->format('d M Y') }}</td>
|
paginate(link.href);
|
||||||
</tr>
|
}
|
||||||
@endforeach
|
"
|
||||||
|
>
|
||||||
|
<div x-show="loading" class="text-center py-4 text-gray-500 dark:text-gray-400">
|
||||||
|
{{ __('Loading...') }}
|
||||||
|
</div>
|
||||||
|
|
||||||
@if ($users->isEmpty())
|
<div x-ref="tableContainer" x-show="!loading">
|
||||||
<tr>
|
@include('users._table')
|
||||||
<td colspan="4" class="px-6 py-4 text-center text-gray-500 dark:text-gray-400">
|
</div>
|
||||||
{{ __('No users found.') }}
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
@endif
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -15,11 +15,33 @@ test('authenticated users can view the users page', function () {
|
|||||||
->assertViewIs('users.index');
|
->assertViewIs('users.index');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('users page lists all users', function () {
|
test('users page passes paginated users to view', function () {
|
||||||
$users = User::factory()->count(3)->create();
|
$user = User::factory()->create();
|
||||||
|
|
||||||
$this->actingAs($users->first())
|
$this->actingAs($user)
|
||||||
->get('/users')
|
->get('/users')
|
||||||
->assertOk()
|
->assertOk()
|
||||||
->assertViewHas('users', fn ($viewUsers) => $viewUsers->count() === User::count());
|
->assertViewHas('users');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ajax request returns table partial', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->withHeader('X-Requested-With', 'XMLHttpRequest')
|
||||||
|
->get('/users')
|
||||||
|
->assertOk()
|
||||||
|
->assertViewIs('users._table');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ajax pagination returns correct page', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
User::factory()->count(15)->create();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->withHeader('X-Requested-With', 'XMLHttpRequest')
|
||||||
|
->get('/users?page=2')
|
||||||
|
->assertOk()
|
||||||
|
->assertViewIs('users._table')
|
||||||
|
->assertViewHas('users', fn ($users) => $users->currentPage() === 2);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user