Skip to content

Commit fb52fa4

Browse files
committed
Add admin user management with Livewire and views
Introduces a UserController and Livewire UserList component for admin user management. Adds Blade views for listing, creating, and editing users, including role assignment and user statistics. Updates navigation and layout to link to the new user management pages. Also standardizes post-auth redirects to the 'home' route.
1 parent 8a210d1 commit fb52fa4

File tree

13 files changed

+787
-10
lines changed

13 files changed

+787
-10
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
class UserController extends Controller
8+
{
9+
/**
10+
* Display a listing of the resource.
11+
*/
12+
public function index()
13+
{
14+
return view('admin.users.index');
15+
}
16+
17+
/**
18+
* Show the form for creating a new resource.
19+
*/
20+
public function create()
21+
{
22+
return view('admin.users.create');
23+
}
24+
25+
/**
26+
* Store a newly created resource in storage.
27+
*/
28+
public function store(Request $request)
29+
{
30+
//
31+
}
32+
33+
/**
34+
* Display the specified resource.
35+
*/
36+
public function show(string $id)
37+
{
38+
//
39+
}
40+
41+
/**
42+
* Show the form for editing the specified resource.
43+
*/
44+
public function edit(string $id)
45+
{
46+
return view('admin.users.edit');
47+
}
48+
49+
/**
50+
* Update the specified resource in storage.
51+
*/
52+
public function update(Request $request, string $id)
53+
{
54+
//
55+
}
56+
57+
/**
58+
* Remove the specified resource from storage.
59+
*/
60+
public function destroy(string $id)
61+
{
62+
//
63+
}
64+
}

app/Livewire/Admin/UserList.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace App\Livewire\Admin;
4+
5+
use Livewire\Component;
6+
use App\Models\User;
7+
use Carbon\Carbon;
8+
use Livewire\WithPagination;
9+
10+
class UserList extends Component
11+
{
12+
use WithPagination;
13+
14+
public $totalUsers;
15+
public $usersThisMonth;
16+
public $usersLastMonth;
17+
public $activeUsers;
18+
public $admins;
19+
public $adminPercent;
20+
public $search = '';
21+
public $sortField = 'name';
22+
public $sortDirection = 'asc';
23+
public $showDeleteModal = false;
24+
public $userToDelete = null;
25+
26+
public function mount()
27+
{
28+
$this->loadStats();
29+
}
30+
31+
public function loadStats()
32+
{
33+
$this->totalUsers = User::count();
34+
35+
$this->usersThisMonth = User::where('created_at', '>=', Carbon::now()->startOfMonth())->count();
36+
37+
$this->usersLastMonth = User::whereBetween('created_at', [
38+
Carbon::now()->subMonth()->startOfMonth(),
39+
Carbon::now()->subMonth()->endOfMonth(),
40+
])->count();
41+
42+
// Example: Active users (adjust to your logic)
43+
$this->activeUsers = User::where('is_active', true)->count();
44+
45+
// Example: Count admins (if using Spatie roles)
46+
$this->admins = User::role('admin')->count();
47+
48+
$this->adminPercent = $this->totalUsers > 0
49+
? round(($this->admins / $this->totalUsers) * 100, 1)
50+
: 0;
51+
}
52+
53+
public function render()
54+
{
55+
$users = User::query()
56+
->with('roles')
57+
->when($this->search, function ($query) {
58+
$query->where('name', 'like', '%' . $this->search . '%')
59+
->orWhere('email', 'like', '%' . $this->search . '%');
60+
})
61+
->orderBy($this->sortField, $this->sortDirection)
62+
->paginate(10)
63+
->withQueryString();
64+
65+
return view('livewire.admin.user-list', ['users' => $users]);
66+
}
67+
}

app/Livewire/Auth/ConfirmPassword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public function confirmPassword(): void
3232

3333
session(['auth.password_confirmed_at' => time()]);
3434

35-
$this->redirectIntended(default: route('dashboard', absolute: false), navigate: true);
35+
$this->redirectIntended(default: route('home', absolute: false), navigate: true);
3636
}
3737
}

app/Livewire/Auth/Login.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function login(): void
4343
RateLimiter::clear($this->throttleKey());
4444
Session::regenerate();
4545

46-
$this->redirectIntended(default: route('dashboard', absolute: false), navigate: true);
46+
$this->redirectIntended(default: route('home', absolute: false), navigate: true);
4747
}
4848

4949
/**
@@ -72,6 +72,6 @@ protected function ensureIsNotRateLimited(): void
7272
*/
7373
protected function throttleKey(): string
7474
{
75-
return Str::transliterate(Str::lower($this->email).'|'.request()->ip());
75+
return Str::transliterate(Str::lower($this->email) . '|' . request()->ip());
7676
}
7777
}

app/Livewire/Auth/Register.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function register(): void
2828
{
2929
$validated = $this->validate([
3030
'name' => ['required', 'string', 'max:255'],
31-
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
31+
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:' . User::class],
3232
'password' => ['required', 'string', 'confirmed', Rules\Password::defaults()],
3333
]);
3434

@@ -38,6 +38,6 @@ public function register(): void
3838

3939
Auth::login($user);
4040

41-
$this->redirect(route('dashboard', absolute: false), navigate: true);
41+
$this->redirect(route('home', absolute: false), navigate: true);
4242
}
4343
}

app/Livewire/Auth/VerifyEmail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class VerifyEmail extends Component
1717
public function sendVerification(): void
1818
{
1919
if (Auth::user()->hasVerifiedEmail()) {
20-
$this->redirectIntended(default: route('dashboard', absolute: false), navigate: true);
20+
$this->redirectIntended(default: route('home', absolute: false), navigate: true);
2121

2222
return;
2323
}

0 commit comments

Comments
 (0)