lt;pgt;Trong các bài trước, chúng ta đã xây dựng lt;stronggt;CRUD Categorieslt;/stronggt; và lt;stronggt;CRUD Postslt;/stronggt;.lt;/pgt;lt;pgt;Ở bài này, chúng ta tiếp tục xây dựng một chức năng rất quan trọng của Blog CMS:lt;/pgt;lt;blockquotegt;lt;pgt;👤 lt;stronggt;Quản lý người dùng — User Managementlt;/stronggt;lt;/pgt;lt;/blockquotegt;lt;pgt;Sau bài học, trang quản trị có thể:lt;/pgt;lt;ulgt;lt;ligt;lt;pgt;👥 Hiển thị danh sách Userlt;/pgt;lt;/ligt;lt;ligt;lt;pgt;🔎 Tìm kiếm Userlt;/pgt;lt;/ligt;lt;ligt;lt;pgt;🛡️ Đổi Rolelt;/pgt;lt;/ligt;lt;ligt;lt;pgt;🔒 Khóa / mở khóa tài khoảnlt;/pgt;lt;/ligt;lt;ligt;lt;pgt;🔑 Reset Passwordlt;/pgt;lt;/ligt;lt;ligt;lt;pgt;🗑️ Xóa Userlt;/pgt;lt;/ligt;lt;ligt;lt;pgt;📄 Phân tranglt;/pgt;lt;/ligt;lt;ligt;lt;pgt;🔗 Kết hợp với Middleware / Authorizationlt;/pgt;lt;div class=quot;separatorquot; style=quot;clear: both; text-align: center;quot;gt;lt;a href=quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFR1Un0krntdBKrt6lvILGrntKAog2i8cqaDRgI_P-8i7uq0ixHprLGhgbvUhopnXg6OnHYvzfkI1ojMn4UyTc6oFS9khpvHOSCwZh-cf80qK1S4Qbay6M5P5RJm_sBO4RIMn2f-c3tVV_oBTa_18l5V7dqtvkeZTOwcuKb__3RzVAeqBi00ZWOeq818M/s1536/laravel-nui.pngquot; imageanchor=quot;1quot; style=quot;margin-left: 1em; margin-right: 1em;quot;gt;lt;img border=quot;0quot; data-original-height=quot;1024quot; data-original-width=quot;1536quot; height=quot;213quot; src=quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFR1Un0krntdBKrt6lvILGrntKAog2i8cqaDRgI_P-8i7uq0ixHprLGhgbvUhopnXg6OnHYvzfkI1ojMn4UyTc6oFS9khpvHOSCwZh-cf80qK1S4Qbay6M5P5RJm_sBO4RIMn2f-c3tVV_oBTa_18l5V7dqtvkeZTOwcuKb__3RzVAeqBi00ZWOeq818M/s320/laravel-nui.pngquot; width=quot;320quot; /gt;lt;/agt;lt;spangt;lt;a name=#39;more#39;gt;lt;/agt;lt;/spangt;lt;/divgt;lt;pgt;lt;/pgt;lt;/ligt;lt;/ulgt;lt;hr /gt;lt;h1gt;1. Mục tiêu bài họclt;/h1gt;lt;pgt;Chúng ta sẽ xây dựng trang:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;/admin/users
lt;/codegt;lt;/pregt;lt;pgt;Giao diện quản trị dự kiến:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;┌──────────────────────────────────────────────────────────────┐
│ 👥 Quản lý Users │
├──────────────────────────────────────────────────────────────┤
│ 🔍 [ Tìm kiếm... ] [ + Thêm User ] │
├────┬────────────────┬─────────────────┬─────────┬────────────┤
│ # │ User │ Email │ Role │ Trạng thái │
├────┼────────────────┼─────────────────┼─────────┼────────────┤
│ 1 │ Admin │ admin@gmail.com │ Admin │ 🟢 Active │
│ 2 │ Nguyễn Văn A │ a@gmail.com │ User │ 🔴 Locked │
│ 3 │ Trần Văn B │ b@gmail.com │ Editor │ 🟢 Active │
└────┴────────────────┴─────────────────┴─────────┴────────────┘
lt;/codegt;lt;/pregt;lt;pgt;Người quản trị có thể thực hiện:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;User
│
├── Xem danh sách
├── Tìm kiếm
├── Đổi Role
├── Khóa tài khoản
├── Mở khóa
├── Reset Password
└── Xóa
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;2. Kiến trúc User Managementlt;/h1gt;lt;pgt;Chức năng User Management vẫn tuân theo mô hình MVC:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Browser
│
▼
Route
│
▼
UserController
│
▼
User Model
│
▼
MySQL
lt;/codegt;lt;/pregt;lt;pgt;Sau đó Controller trả dữ liệu về Blade:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;UserController
│
▼
resources/views/users/
│
├── index.blade.php
├── edit.blade.php
└── ...
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;3. Kiểm tra bảng userslt;/h1gt;lt;pgt;Laravel đã có bảng lt;code inline=quot;quot;gt;userslt;/codegt; từ lúc cài đặt Breeze.lt;/pgt;lt;pgt;Thông thường bảng có các trường:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;users
├── id
├── name
├── email
├── email_verified_at
├── password
├── remember_token
├── created_at
└── updated_at
lt;/codegt;lt;/pregt;lt;pgt;Tuy nhiên Blog CMS cần thêm thông tin quản trị.lt;/pgt;lt;pgt;Chúng ta cần ít nhất:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;role
is_active
lt;/codegt;lt;/pregt;lt;pgt;Trong đó:lt;/pgt;lt;h3gt;lt;code inline=quot;quot;gt;rolelt;/codegt;lt;/h3gt;lt;pgt;Xác định quyền của User:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;admin
editor
user
lt;/codegt;lt;/pregt;lt;h3gt;lt;code inline=quot;quot;gt;is_activelt;/codegt;lt;/h3gt;lt;pgt;Xác định tài khoản có đang hoạt động hay không:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;1 = Active
0 = Locked
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;4. Tạo migration thêm role và is_activelt;/h1gt;lt;pgt;Nếu project của bạn chưa có hai trường này, chạy:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan make:migration add_role_and_is_active_to_users_table --table=users
lt;/codegt;lt;/pregt;lt;pgt;Mở migration vừa tạo:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;lt;?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table(#39;users#39;, function (Blueprint $table) {
$table-gt;string(#39;role#39;)
-gt;default(#39;user#39;)
-gt;after(#39;email#39;);
$table-gt;boolean(#39;is_active#39;)
-gt;default(true)
-gt;after(#39;role#39;);
});
}
public function down(): void
{
Schema::table(#39;users#39;, function (Blueprint $table) {
$table-gt;dropColumn([
#39;role#39;,
#39;is_active#39;,
]);
});
}
};
lt;/codegt;lt;/pregt;lt;pgt;Sau đó:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan migrate
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;5. Cấu trúc bảng Userslt;/h1gt;lt;pgt;Sau migration:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;users
│
├── id
├── name
├── email
├── role
├── is_active
├── email_verified_at
├── password
├── remember_token
├── created_at
└── updated_at
lt;/codegt;lt;/pregt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;1 | Admin | admin@gmail.com | admin | 1
2 | Nguyễn A | a@gmail.com | user | 1
3 | Trần B | b@gmail.com | editor | 0
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;6. Cập nhật User Modellt;/h1gt;lt;pgt;Mở:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;app/Models/User.php
lt;/codegt;lt;/pregt;lt;pgt;Thêm lt;code inline=quot;quot;gt;rolelt;/codegt; và lt;code inline=quot;quot;gt;is_activelt;/codegt; vào lt;code inline=quot;quot;gt;$fillablelt;/codegt;.lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;protected $fillable = [
#39;name#39;,
#39;email#39;,
#39;password#39;,
#39;role#39;,
#39;is_active#39;,
];
lt;/codegt;lt;/pregt;lt;pgt;Laravel 12 cũng hỗ trợ khai báo cast theo cách hiện đại bằng lt;code inline=quot;quot;gt;casts()lt;/codegt;.lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;protected function casts(): array
{
return [
#39;email_verified_at#39; =gt; #39;datetime#39;,
#39;password#39; =gt; #39;hashed#39;,
#39;is_active#39; =gt; #39;boolean#39;,
];
}
lt;/codegt;lt;/pregt;lt;pgt;Như vậy:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;$user-gt;is_active
lt;/codegt;lt;/pregt;lt;pgt;sẽ trả về:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;true
lt;/codegt;lt;/pregt;lt;pgt;hoặc:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;false
lt;/codegt;lt;/pregt;lt;pgt;thay vì chỉ làm việc với lt;code inline=quot;quot;gt;0lt;/codegt; và lt;code inline=quot;quot;gt;1lt;/codegt;.lt;/pgt;lt;hr /gt;lt;h1gt;7. Tạo User Controllerlt;/h1gt;lt;pgt;Chúng ta sử dụng Resource Controller:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan make:controller UserController --resource
lt;/codegt;lt;/pregt;lt;pgt;Laravel tạo:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;app/Http/Controllers/UserController.php
lt;/codegt;lt;/pregt;lt;pgt;Resource Controller có các method:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;index()
create()
store()
show()
edit()
update()
destroy()
lt;/codegt;lt;/pregt;lt;pgt;Trong bài này chúng ta chủ yếu sử dụng:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;index()
edit()
update()
destroy()
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;8. Import User Modellt;/h1gt;lt;pgt;Mở:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;app/Http/Controllers/UserController.php
lt;/codegt;lt;/pregt;lt;pgt;Thêm:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;9. Hiển thị danh sách Userslt;/h1gt;lt;pgt;Method lt;code inline=quot;quot;gt;index()lt;/codegt;:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;public function index(Request $request)
{
$search = $request-gt;input(#39;search#39;);
$users = User::query()
-gt;when($search, function ($query, $search) {
$query-gt;where(#39;name#39;, #39;like#39;, quot;%{$search}%quot;)
-gt;orWhere(#39;email#39;, #39;like#39;, quot;%{$search}%quot;);
})
-gt;latest()
-gt;paginate(10)
-gt;withQueryString();
return view(#39;users.index#39;, compact(#39;users#39;));
}
lt;/codegt;lt;/pregt;lt;pgt;Ở đây chúng ta sử dụng:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;when()
lt;/codegt;lt;/pregt;lt;pgt;để chỉ tìm kiếm khi người dùng nhập từ khóa.lt;/pgt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;/admin/users?search=nguyen
lt;/codegt;lt;/pregt;lt;pgt;Laravel sẽ tìm:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;name LIKE #39;%nguyen%#39;
lt;/codegt;lt;/pregt;lt;pgt;hoặc:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;email LIKE #39;%nguyen%#39;
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;10. Routelt;/h1gt;lt;pgt;Mở:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;routes/web.php
lt;/codegt;lt;/pregt;lt;pgt;Thêm:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;use App\Http\Controllers\UserController;
Route::middleware(#39;auth#39;)-gt;group(function () {
Route::resource(#39;users#39;, UserController::class);
});
lt;/codegt;lt;/pregt;lt;pgt;Nếu muốn URL có dạng:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;/admin/users
lt;/codegt;lt;/pregt;lt;pgt;thì nên tổ chức route:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;Route::middleware(#39;auth#39;)
-gt;prefix(#39;admin#39;)
-gt;name(#39;admin.#39;)
-gt;group(function () {
Route::resource(#39;users#39;, UserController::class);
});
lt;/codegt;lt;/pregt;lt;pgt;Khi đó:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;GET /admin/users
GET /admin/users/create
POST /admin/users
GET /admin/users/{user}
GET /admin/users/{user}/edit
PUT/PATCH /admin/users/{user}
DELETE /admin/users/{user}
lt;/codegt;lt;/pregt;lt;pgt;Tên route:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;admin.users.index
admin.users.create
admin.users.store
admin.users.show
admin.users.edit
admin.users.update
admin.users.destroy
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;11. View danh sách Userslt;/h1gt;lt;pgt;Tạo:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;resources/views/users/index.blade.php
lt;/codegt;lt;/pregt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-bladequot;gt;lt;x-app-layoutgt;
lt;div class=quot;max-w-7xl mx-auto py-8quot;gt;
lt;x-ui.card
title=quot;Quản lý Usersquot;
description=quot;Quản lý tài khoản người dùngquot;gt;
{{-- SEARCH --}}
lt;form
action=quot;{{ route(#39;admin.users.index#39;) }}quot;
method=quot;GETquot;
class=quot;mb-6quot;gt;
lt;div class=quot;flex gap-2quot;gt;
lt;input
type=quot;textquot;
name=quot;searchquot;
value=quot;{{ request(#39;search#39;) }}quot;
placeholder=quot;Tìm theo tên hoặc email...quot;
class=quot;flex-1 rounded-lg border-gray-300quot;gt;
lt;button
type=quot;submitquot;
class=quot;px-4 py-2 bg-gray-800 text-white rounded-lgquot;gt;
🔍 Tìm kiếm
lt;/buttongt;
lt;/divgt;
lt;/formgt;
{{-- TABLE --}}
lt;div class=quot;overflow-x-autoquot;gt;
lt;table class=quot;w-full text-smquot;gt;
lt;theadgt;
lt;tr class=quot;border-bquot;gt;
lt;th class=quot;text-left p-3quot;gt;
#
lt;/thgt;
lt;th class=quot;text-left p-3quot;gt;
User
lt;/thgt;
lt;th class=quot;text-left p-3quot;gt;
Email
lt;/thgt;
lt;th class=quot;text-left p-3quot;gt;
Role
lt;/thgt;
lt;th class=quot;text-left p-3quot;gt;
Trạng thái
lt;/thgt;
lt;th class=quot;text-right p-3quot;gt;
Thao tác
lt;/thgt;
lt;/trgt;
lt;/theadgt;
lt;tbodygt;
@forelse ($users as $user)
lt;tr class=quot;border-bquot;gt;
lt;td class=quot;p-3quot;gt;
{{ $user-gt;id }}
lt;/tdgt;
lt;td class=quot;p-3 font-semiboldquot;gt;
{{ $user-gt;name }}
lt;/tdgt;
lt;td class=quot;p-3quot;gt;
{{ $user-gt;email }}
lt;/tdgt;
lt;td class=quot;p-3quot;gt;
@if ($user-gt;role === #39;admin#39;)
🛡️ Admin
@elseif ($user-gt;role === #39;editor#39;)
✏️ Editor
@else
👤 User
@endif
lt;/tdgt;
lt;td class=quot;p-3quot;gt;
@if ($user-gt;is_active)
lt;span class=quot;text-green-600quot;gt;
🟢 Active
lt;/spangt;
@else
lt;span class=quot;text-red-600quot;gt;
🔴 Locked
lt;/spangt;
@endif
lt;/tdgt;
lt;td class=quot;p-3quot;gt;
lt;div class=quot;flex justify-end gap-2quot;gt;
lt;a
href=quot;{{ route(#39;admin.users.edit#39;, $user) }}quot;
class=quot;px-3 py-1 bg-blue-600 text-white roundedquot;gt;
Sửa
lt;/agt;
lt;form
action=quot;{{ route(#39;admin.users.destroy#39;, $user) }}quot;
method=quot;POSTquot;
onsubmit=quot;return confirm(#39;Bạn có chắc muốn xóa User này?#39;)quot;gt;
@csrf
@method(#39;DELETE#39;)
lt;button
type=quot;submitquot;
class=quot;px-3 py-1 bg-red-600 text-white roundedquot;gt;
Xóa
lt;/buttongt;
lt;/formgt;
lt;/divgt;
lt;/tdgt;
lt;/trgt;
@empty
lt;trgt;
lt;td
colspan=quot;6quot;
class=quot;p-6 text-center text-gray-500quot;gt;
Không tìm thấy User.
lt;/tdgt;
lt;/trgt;
@endforelse
lt;/tbodygt;
lt;/tablegt;
lt;/divgt;
{{-- PAGINATION --}}
lt;div class=quot;mt-6quot;gt;
{{ $users-gt;links() }}
lt;/divgt;
lt;/x-ui.cardgt;
lt;/divgt;
lt;/x-app-layoutgt;
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;12. Tìm kiếm Userlt;/h1gt;lt;pgt;Form:lt;/pgt;lt;pregt;lt;code class=quot;language-bladequot;gt;lt;form method=quot;GETquot;gt;
lt;/codegt;lt;/pregt;lt;pgt;sẽ gửi:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;/admin/users?search=admin
lt;/codegt;lt;/pregt;lt;pgt;Controller nhận:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;$search = $request-gt;input(#39;search#39;);
lt;/codegt;lt;/pregt;lt;pgt;Sau đó:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;-gt;when($search, function ($query, $search) {
$query-gt;where(#39;name#39;, #39;like#39;, quot;%{$search}%quot;)
-gt;orWhere(#39;email#39;, #39;like#39;, quot;%{$search}%quot;);
})
lt;/codegt;lt;/pregt;lt;pgt;Ví dụ database:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Nguyễn Văn A
admin@gmail.com
editor@gmail.com
lt;/codegt;lt;/pregt;lt;pgt;Tìm:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;admin
lt;/codegt;lt;/pregt;lt;pgt;sẽ trả về:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;admin@gmail.com
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;13. Chỉnh sửa Userlt;/h1gt;lt;pgt;Method:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;public function edit(User $user)
{
return view(#39;users.edit#39;, compact(#39;user#39;));
}
lt;/codegt;lt;/pregt;lt;pgt;Laravel tự động Route Model Binding:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;User $user
lt;/codegt;lt;/pregt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;/admin/users/5/edit
lt;/codegt;lt;/pregt;lt;pgt;Laravel tự tìm:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;User::findOrFail(5);
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;14. Form Edit Userlt;/h1gt;lt;pgt;Tạo:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;resources/views/users/edit.blade.php
lt;/codegt;lt;/pregt;lt;pregt;lt;code class=quot;language-bladequot;gt;lt;x-app-layoutgt;
lt;div class=quot;max-w-3xl mx-auto py-8quot;gt;
lt;x-ui.card
title=quot;Chỉnh sửa Userquot;
description=quot;Cập nhật thông tin tài khoảnquot;gt;
lt;form
action=quot;{{ route(#39;admin.users.update#39;, $user) }}quot;
method=quot;POSTquot;gt;
@csrf
@method(#39;PUT#39;)
{{-- NAME --}}
lt;div class=quot;mb-4quot;gt;
lt;label class=quot;block font-semibold mb-2quot;gt;
Họ tên
lt;/labelgt;
lt;input
type=quot;textquot;
name=quot;namequot;
value=quot;{{ old(#39;name#39;, $user-gt;name) }}quot;
class=quot;w-full rounded-lg border-gray-300quot;gt;
@error(#39;name#39;)
lt;p class=quot;text-red-600 text-sm mt-1quot;gt;
{{ $message }}
lt;/pgt;
@enderror
lt;/divgt;
{{-- EMAIL --}}
lt;div class=quot;mb-4quot;gt;
lt;label class=quot;block font-semibold mb-2quot;gt;
Email
lt;/labelgt;
lt;input
type=quot;emailquot;
name=quot;emailquot;
value=quot;{{ old(#39;email#39;, $user-gt;email) }}quot;
class=quot;w-full rounded-lg border-gray-300quot;gt;
@error(#39;email#39;)
lt;p class=quot;text-red-600 text-sm mt-1quot;gt;
{{ $message }}
lt;/pgt;
@enderror
lt;/divgt;
{{-- ROLE --}}
lt;div class=quot;mb-4quot;gt;
lt;label class=quot;block font-semibold mb-2quot;gt;
Role
lt;/labelgt;
lt;select
name=quot;rolequot;
class=quot;w-full rounded-lg border-gray-300quot;gt;
lt;option
value=quot;userquot;
@selected(old(#39;role#39;, $user-gt;role) === #39;user#39;)gt;
👤 User
lt;/optiongt;
lt;option
value=quot;editorquot;
@selected(old(#39;role#39;, $user-gt;role) === #39;editor#39;)gt;
✏️ Editor
lt;/optiongt;
lt;option
value=quot;adminquot;
@selected(old(#39;role#39;, $user-gt;role) === #39;admin#39;)gt;
🛡️ Admin
lt;/optiongt;
lt;/selectgt;
lt;/divgt;
{{-- STATUS --}}
lt;div class=quot;mb-6quot;gt;
lt;label class=quot;block font-semibold mb-2quot;gt;
Trạng thái
lt;/labelgt;
lt;label class=quot;flex items-center gap-2quot;gt;
lt;input
type=quot;checkboxquot;
name=quot;is_activequot;
value=quot;1quot;
@checked(old(#39;is_active#39;, $user-gt;is_active))gt;
lt;spangt;
🟢 Tài khoản đang hoạt động
lt;/spangt;
lt;/labelgt;
lt;/divgt;
{{-- BUTTON --}}
lt;div class=quot;flex gap-2quot;gt;
lt;button
type=quot;submitquot;
class=quot;px-5 py-2 bg-blue-600 text-white rounded-lgquot;gt;
💾 Lưu thay đổi
lt;/buttongt;
lt;a
href=quot;{{ route(#39;admin.users.index#39;) }}quot;
class=quot;px-5 py-2 bg-gray-200 rounded-lgquot;gt;
Hủy
lt;/agt;
lt;/divgt;
lt;/formgt;
lt;/x-ui.cardgt;
lt;/divgt;
lt;/x-app-layoutgt;
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;15. Validationlt;/h1gt;lt;pgt;Trong lt;code inline=quot;quot;gt;update()lt;/codegt;:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;$request-gt;validate([
#39;name#39; =gt; [
#39;required#39;,
#39;string#39;,
#39;max:255#39;,
],
#39;email#39; =gt; [
#39;required#39;,
#39;email#39;,
#39;max:255#39;,
#39;unique:users,email,#39; . $user-gt;id,
],
#39;role#39; =gt; [
#39;required#39;,
#39;in:admin,editor,user#39;,
],
#39;is_active#39; =gt; [
#39;nullable#39;,
#39;boolean#39;,
],
]);
lt;/codegt;lt;/pregt;lt;pgt;Điểm quan trọng:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;#39;unique:users,email,#39; . $user-gt;id
lt;/codegt;lt;/pregt;lt;pgt;cho phép User giữ nguyên email hiện tại.lt;/pgt;lt;pgt;Ví dụ User:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;id = 5
email = admin@gmail.com
lt;/codegt;lt;/pregt;lt;pgt;khi sửa User số 5 vẫn được phép giữ:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;admin@gmail.com
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;16. Cập nhật Userlt;/h1gt;lt;pgt;Method hoàn chỉnh:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;public function update(Request $request, User $user)
{
$validated = $request-gt;validate([
#39;name#39; =gt; [
#39;required#39;,
#39;string#39;,
#39;max:255#39;,
],
#39;email#39; =gt; [
#39;required#39;,
#39;email#39;,
#39;max:255#39;,
#39;unique:users,email,#39; . $user-gt;id,
],
#39;role#39; =gt; [
#39;required#39;,
#39;in:admin,editor,user#39;,
],
#39;is_active#39; =gt; [
#39;nullable#39;,
#39;boolean#39;,
],
]);
$validated[#39;is_active#39;] = $request-gt;boolean(#39;is_active#39;);
$user-gt;update($validated);
return redirect()
-gt;route(#39;admin.users.index#39;)
-gt;with(#39;success#39;, #39;Cập nhật User thành công.#39;);
}
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;17. Khóa tài khoảnlt;/h1gt;lt;pgt;Đây là chức năng rất quan trọng.lt;/pgt;lt;pgt;Không nhất thiết phải xóa User.lt;/pgt;lt;pgt;Thay vào đó:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;is_active = 0
lt;/codegt;lt;/pregt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Nguyễn Văn A
email: a@gmail.com
role: user
is_active: 0
lt;/codegt;lt;/pregt;lt;pgt;Tài khoản vẫn tồn tại trong database nhưng bị khóa.lt;/pgt;lt;hr /gt;lt;h1gt;18. Kiểm tra tài khoản bị khóalt;/h1gt;lt;pgt;Chúng ta có thể kiểm tra trong Middleware.lt;/pgt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;if (! auth()-gt;user()-gt;is_active) {
auth()-gt;logout();
return redirect()
-gt;route(#39;login#39;)
-gt;withErrors([
#39;email#39; =gt; #39;Tài khoản của bạn đã bị khóa.#39;,
]);
}
lt;/codegt;lt;/pregt;lt;pgt;Tuy nhiên với hệ thống thực tế, nên tạo Middleware riêng.lt;/pgt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan make:middleware CheckUserActive
lt;/codegt;lt;/pregt;lt;pgt;Sau đó đưa logic kiểm tra tài khoản vào Middleware.lt;/pgt;lt;pgt;Đây sẽ là nền tảng tốt để phát triển hệ thống quản trị hoàn chỉnh.lt;/pgt;lt;hr /gt;lt;h1gt;19. Reset Passwordlt;/h1gt;lt;pgt;Admin cũng cần khả năng reset password cho User.lt;/pgt;lt;pgt;Laravel cung cấp:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;Hash::make()
lt;/codegt;lt;/pregt;lt;pgt;để hash password.lt;/pgt;lt;pgt;Import:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;use Illuminate\Support\Facades\Hash;
lt;/codegt;lt;/pregt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;$user-gt;update([
#39;password#39; =gt; Hash::make(#39;12345678#39;),
]);
lt;/codegt;lt;/pregt;lt;pgt;Tuy nhiên lt;stronggt;không nên hard-code password mặc địnhlt;/stronggt; trong hệ thống thực tế.lt;/pgt;lt;pgt;Tốt hơn là Admin nhập password mới.lt;/pgt;lt;hr /gt;lt;h1gt;20. Thêm Reset Password vào Formlt;/h1gt;lt;pgt;Trong form edit:lt;/pgt;lt;pregt;lt;code class=quot;language-bladequot;gt;lt;div class=quot;mb-6quot;gt;
lt;label class=quot;block font-semibold mb-2quot;gt;
Password mới
lt;/labelgt;
lt;input
type=quot;passwordquot;
name=quot;passwordquot;
class=quot;w-full rounded-lg border-gray-300quot;
placeholder=quot;Để trống nếu không muốn đổiquot;gt;
@error(#39;password#39;)
lt;p class=quot;text-red-600 text-sm mt-1quot;gt;
{{ $message }}
lt;/pgt;
@enderror
lt;/divgt;
lt;/codegt;lt;/pregt;lt;pgt;Validation:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;#39;password#39; =gt; [
#39;nullable#39;,
#39;string#39;,
#39;min:8#39;,
#39;confirmed#39;,
],
lt;/codegt;lt;/pregt;lt;pgt;Form cần thêm:lt;/pgt;lt;pregt;lt;code class=quot;language-bladequot;gt;lt;input
type=quot;passwordquot;
name=quot;password_confirmationquot;
class=quot;w-full rounded-lg border-gray-300quot;gt;
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;21. Cập nhật Password an toànlt;/h1gt;lt;pgt;Không nên làm:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;$user-gt;password = $request-gt;password;
lt;/codegt;lt;/pregt;lt;pgt;vì password sẽ được lưu trực tiếp.lt;/pgt;lt;pgt;Nên dùng:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;$user-gt;password = Hash::make($request-gt;password);
lt;/codegt;lt;/pregt;lt;pgt;Hoặc Laravel có thể tự hash nếu Model đã khai báo:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;#39;password#39; =gt; #39;hashed#39;,
lt;/codegt;lt;/pregt;lt;pgt;trong lt;code inline=quot;quot;gt;casts()lt;/codegt;.lt;/pgt;lt;pgt;Khi đó:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;$user-gt;update([
#39;password#39; =gt; $request-gt;password,
]);
lt;/codegt;lt;/pregt;lt;pgt;Laravel sẽ xử lý việc hash.lt;/pgt;lt;hr /gt;lt;h1gt;22. Hoàn chỉnh method update()lt;/h1gt;lt;pgt;Có thể viết:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;public function update(Request $request, User $user)
{
$validated = $request-gt;validate([
#39;name#39; =gt; [
#39;required#39;,
#39;string#39;,
#39;max:255#39;,
],
#39;email#39; =gt; [
#39;required#39;,
#39;email#39;,
#39;max:255#39;,
#39;unique:users,email,#39; . $user-gt;id,
],
#39;role#39; =gt; [
#39;required#39;,
#39;in:admin,editor,user#39;,
],
#39;password#39; =gt; [
#39;nullable#39;,
#39;string#39;,
#39;min:8#39;,
#39;confirmed#39;,
],
#39;is_active#39; =gt; [
#39;nullable#39;,
#39;boolean#39;,
],
]);
$validated[#39;is_active#39;] = $request-gt;boolean(#39;is_active#39;);
if (empty($validated[#39;password#39;])) {
unset($validated[#39;password#39;]);
}
$user-gt;update($validated);
return redirect()
-gt;route(#39;admin.users.index#39;)
-gt;with(#39;success#39;, #39;Cập nhật User thành công.#39;);
}
lt;/codegt;lt;/pregt;lt;pgt;Nhờ:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;unset($validated[#39;password#39;]);
lt;/codegt;lt;/pregt;lt;pgt;nếu Admin không nhập password mới thì password cũ vẫn được giữ nguyên.lt;/pgt;lt;hr /gt;lt;h1gt;23. Xóa Userlt;/h1gt;lt;pgt;Method:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;public function destroy(User $user)
{
$user-gt;delete();
return redirect()
-gt;route(#39;admin.users.index#39;)
-gt;with(#39;success#39;, #39;Đã xóa User.#39;);
}
lt;/codegt;lt;/pregt;lt;pgt;Form:lt;/pgt;lt;pregt;lt;code class=quot;language-bladequot;gt;lt;form
action=quot;{{ route(#39;admin.users.destroy#39;, $user) }}quot;
method=quot;POSTquot;gt;
@csrf
@method(#39;DELETE#39;)
lt;button type=quot;submitquot;gt;
🗑️ Xóa
lt;/buttongt;
lt;/formgt;
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;24. Không nên cho Admin tự xóa chính mìnhlt;/h1gt;lt;pgt;Đây là một lỗi logic rất dễ xảy ra.lt;/pgt;lt;pgt;Ví dụ Admin:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;admin@gmail.com
lt;/codegt;lt;/pregt;lt;pgt;đăng nhập vào Dashboard.lt;/pgt;lt;pgt;Sau đó bấm:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Xóa
lt;/codegt;lt;/pregt;lt;pgt;chính tài khoản của mình.lt;/pgt;lt;pgt;Kết quả:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;User bị xóa
↓
Session vẫn đang tồn tại
↓
Hệ thống có thể phát sinh lỗi
lt;/codegt;lt;/pregt;lt;pgt;Do đó nên kiểm tra:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;if ($user-gt;id === auth()-gt;id()) {
return back()
-gt;with(#39;error#39;, #39;Bạn không thể xóa chính tài khoản của mình.#39;);
}
lt;/codegt;lt;/pregt;lt;pgt;Method:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;public function destroy(User $user)
{
if ($user-gt;id === auth()-gt;id()) {
return back()
-gt;with(#39;error#39;, #39;Bạn không thể xóa chính tài khoản của mình.#39;);
}
$user-gt;delete();
return redirect()
-gt;route(#39;admin.users.index#39;)
-gt;with(#39;success#39;, #39;Đã xóa User.#39;);
}
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;25. Không cho Admin tự khóa mìnhlt;/h1gt;lt;pgt;Tương tự:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Admin
↓
Khóa tài khoản
↓
is_active = false
lt;/codegt;lt;/pregt;lt;pgt;Sau đó chính Admin sẽ không thể đăng nhập nữa.lt;/pgt;lt;pgt;Vì vậy cần ngăn:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;if ($user-gt;id === auth()-gt;id()) {
...
}
lt;/codegt;lt;/pregt;lt;pgt;Đây là một nguyên tắc quan trọng trong Admin CMS:lt;/pgt;lt;blockquotegt;lt;pgt;🔐 Không cho tài khoản hiện tại tự vô hiệu hóa quyền truy cập của chính mình.lt;/pgt;lt;/blockquotegt;lt;hr /gt;lt;h1gt;26. Bảo vệ User Managementlt;/h1gt;lt;pgt;Hiện tại:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;Route::middleware(#39;auth#39;)
lt;/codegt;lt;/pregt;lt;pgt;chỉ kiểm tra:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Đã đăng nhập?
lt;/codegt;lt;/pregt;lt;pgt;Nhưng chưa kiểm tra:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Có phải Admin?
lt;/codegt;lt;/pregt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;User thường
↓
/admin/users
↓
❌ Không được phép
lt;/codegt;lt;/pregt;lt;pgt;Trong khi:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Admin
↓
/admin/users
↓
✅ Được phép
lt;/codegt;lt;/pregt;lt;pgt;Đây chính là lý do chúng ta cần:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Authorization
lt;/codegt;lt;/pregt;lt;pgt;đã học ở lt;stronggt;Bài 25lt;/stronggt;.lt;/pgt;lt;hr /gt;lt;h1gt;27. Kết hợp Role với Middlewarelt;/h1gt;lt;pgt;Ví dụ tạo Middleware:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan make:middleware AdminMiddleware
lt;/codegt;lt;/pregt;lt;pgt;Logic:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;public function handle($request, Closure $next)
{
if (! auth()-gt;check()) {
return redirect()-gt;route(#39;login#39;);
}
if (auth()-gt;user()-gt;role !== #39;admin#39;) {
abort(403);
}
return $next($request);
}
lt;/codegt;lt;/pregt;lt;pgt;Sau đó bảo vệ khu vực Admin:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;Route::middleware([#39;auth#39;, #39;admin#39;])
-gt;prefix(#39;admin#39;)
-gt;name(#39;admin.#39;)
-gt;group(function () {
Route::resource(#39;users#39;, UserController::class);
});
lt;/codegt;lt;/pregt;lt;pgt;Khi đó:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Guest
↓
Login
User
↓
403 Forbidden
Editor
↓
403 Forbidden
Admin
↓
✅ User Management
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;28. Sơ đồ quyềnlt;/h1gt;lt;pgt;Sau bài này hệ thống có thể có:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt; ┌─────────────┐
│ ADMIN │
└──────┬──────┘
│
┌─────────────┼──────────────┐
↓ ↓ ↓
Users Posts Categories
│
┌─────┼─────┐
↓ ↓ ↓
Role Lock Reset
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;29. Kiểm tra Routelt;/h1gt;lt;pgt;Chạy:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan route:list
lt;/codegt;lt;/pregt;lt;pgt;Bạn sẽ thấy:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;GET|HEAD admin/users
POST admin/users
GET|HEAD admin/users/{user}/edit
PUT|PATCH admin/users/{user}
DELETE admin/users/{user}
lt;/codegt;lt;/pregt;lt;pgt;Nếu muốn lọc:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan route:list --path=admin
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;30. Tạo dữ liệu User bằng Factorylt;/h1gt;lt;pgt;Nếu muốn kiểm tra giao diện với nhiều User:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan tinker
lt;/codegt;lt;/pregt;lt;pgt;Sau đó:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;User::factory()
-gt;count(30)
-gt;create();
lt;/codegt;lt;/pregt;lt;pgt;Nếu muốn tạo Admin:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;User::factory()-gt;create([
#39;name#39; =gt; #39;Administrator#39;,
#39;email#39; =gt; #39;admin@example.com#39;,
#39;role#39; =gt; #39;admin#39;,
#39;is_active#39; =gt; true,
]);
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;31. Cập nhật UserFactorylt;/h1gt;lt;pgt;Trong:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;database/factories/UserFactory.php
lt;/codegt;lt;/pregt;lt;pgt;có thể thêm:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;#39;role#39; =gt; #39;user#39;,
#39;is_active#39; =gt; true,
lt;/codegt;lt;/pregt;lt;pgt;Ví dụ:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;public function definition(): array
{
return [
#39;name#39; =gt; fake()-gt;name(),
#39;email#39; =gt; fake()-gt;unique()-gt;safeEmail(),
#39;email_verified_at#39; =gt; now(),
#39;password#39; =gt; static::$password
??= Hash::make(#39;password#39;),
#39;remember_token#39; =gt; Str::random(10),
#39;role#39; =gt; #39;user#39;,
#39;is_active#39; =gt; true,
];
}
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;32. Seeder Adminlt;/h1gt;lt;pgt;Một hệ thống CMS nên có ít nhất một Admin.lt;/pgt;lt;pgt;Trong:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;database/seeders/DatabaseSeeder.php
lt;/codegt;lt;/pregt;lt;pgt;có thể tạo:lt;/pgt;lt;pregt;lt;code class=quot;language-phpquot;gt;User::factory()-gt;create([
#39;name#39; =gt; #39;Admin#39;,
#39;email#39; =gt; #39;admin@example.com#39;,
#39;role#39; =gt; #39;admin#39;,
#39;is_active#39; =gt; true,
]);
lt;/codegt;lt;/pregt;lt;pgt;Sau đó:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan db:seed
lt;/codegt;lt;/pregt;lt;pgt;Hoặc:lt;/pgt;lt;pregt;lt;code class=quot;language-bashquot;gt;php artisan migrate:fresh --seed
lt;/codegt;lt;/pregt;lt;pgt;⚠️ lt;code inline=quot;quot;gt;migrate:fresh --seedlt;/codegt; sẽ xóa toàn bộ database rồi tạo lại.lt;/pgt;lt;pgt;Chỉ sử dụng khi đang phát triển hoặc khi bạn thực sự muốn reset database.lt;/pgt;lt;hr /gt;lt;h1gt;33. Những gì chúng ta đã xây dựnglt;/h1gt;lt;pgt;Sau Bài 28:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;👥 USER MANAGEMENT
│
├── Danh sách User
│
├── 🔍 Search
│
├── 📄 Pagination
│
├── ✏️ Edit User
│
├── 🛡️ Đổi Role
│
├── 🔒 Lock / Unlock
│
├── 🔑 Reset Password
│
└── 🗑️ Delete User
lt;/codegt;lt;/pregt;lt;pgt;Database:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;users
│
├── id
├── name
├── email
├── role
├── is_active
├── password
├── created_at
└── updated_at
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;34. Cấu trúc project sau Bài 28lt;/h1gt;lt;pgt;Project Blog CMS lúc này có thể có:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;app/
├── Http/
│ ├── Controllers/
│ │ ├── CategoryController.php
│ │ ├── PostController.php
│ │ └── UserController.php
│ │
│ └── Middleware/
│ ├── AdminMiddleware.php
│ └── CheckUserActive.php
│
├── Models/
│ ├── Category.php
│ ├── Post.php
│ └── User.php
│
resources/
└── views/
├── categories/
│ ├── index.blade.php
│ ├── create.blade.php
│ └── edit.blade.php
│
├── posts/
│ ├── index.blade.php
│ ├── create.blade.php
│ └── edit.blade.php
│
└── users/
├── index.blade.php
└── edit.blade.php
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;35. Luồng hoạt động hoàn chỉnhlt;/h1gt;lt;pgt;Khi Admin truy cập:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;/admin/users
lt;/codegt;lt;/pregt;lt;pgt;Laravel xử lý:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Browser
│
▼
Route
│
▼
auth Middleware
│
▼
admin Middleware
│
▼
UserController@index()
│
▼
User Model
│
▼
MySQL
│
▼
$users
│
▼
users/index.blade.php
│
▼
Browser
lt;/codegt;lt;/pregt;lt;pgt;Khi sửa:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;/admin/users/10/edit
lt;/codegt;lt;/pregt;lt;pgt;luồng:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Route
↓
UserController@edit
↓
User Model
↓
User #10
↓
users/edit.blade.php
lt;/codegt;lt;/pregt;lt;pgt;Khi lưu:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;PUT /admin/users/10
↓
UserController@update
↓
Validation
↓
User::update()
↓
MySQL
↓
Redirect
↓
/admin/users
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;36. Bài tập thực hànhlt;/h1gt;lt;h3gt;Bài tập 1lt;/h3gt;lt;pgt;Tạo trang:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;/admin/users
lt;/codegt;lt;/pregt;lt;pgt;hiển thị:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;ID
Name
Email
Role
Status
Action
lt;/codegt;lt;/pregt;lt;h3gt;Bài tập 2lt;/h3gt;lt;pgt;Thêm tìm kiếm:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Name
Email
lt;/codegt;lt;/pregt;lt;h3gt;Bài tập 3lt;/h3gt;lt;pgt;Thêm:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Pagination 10 User/page
lt;/codegt;lt;/pregt;lt;h3gt;Bài tập 4lt;/h3gt;lt;pgt;Cho phép Admin đổi:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;User
Editor
Admin
lt;/codegt;lt;/pregt;lt;h3gt;Bài tập 5lt;/h3gt;lt;pgt;Thêm:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Active
Locked
lt;/codegt;lt;/pregt;lt;h3gt;Bài tập 6lt;/h3gt;lt;pgt;Không cho Admin:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Tự xóa mình
lt;/codegt;lt;/pregt;lt;h3gt;Bài tập 7lt;/h3gt;lt;pgt;Không cho Admin:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Tự khóa mình
lt;/codegt;lt;/pregt;lt;h3gt;Bài tập 8lt;/h3gt;lt;pgt;Thêm reset password:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Password mới
Confirm Password
lt;/codegt;lt;/pregt;lt;hr /gt;lt;h1gt;🎯 Tổng kết Bài 28lt;/h1gt;lt;pgt;Trong bài này chúng ta đã xây dựng một lt;stronggt;User Management cơ bản cho Blog CMSlt;/stronggt;.lt;/pgt;lt;pgt;Các kiến thức quan trọng:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;Resource Controller
↓
User Model
↓
Validation
↓
Search
↓
Pagination
↓
Role
↓
Account Status
↓
Password Reset
↓
Authorization
lt;/codegt;lt;/pregt;lt;pgt;Điểm quan trọng nhất của bài này không chỉ là CRUD User.lt;/pgt;lt;pgt;Chúng ta bắt đầu chuyển từ:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;CRUD thông thường
lt;/codegt;lt;/pregt;lt;pgt;sang:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;QUẢN TRỊ HỆ THỐNG
lt;/codegt;lt;/pregt;lt;pgt;Một User không còn đơn giản chỉ là:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;name
email
password
lt;/codegt;lt;/pregt;lt;pgt;mà bắt đầu có:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt;ROLE
STATUS
PERMISSION
AUTHORIZATION
lt;/codegt;lt;/pregt;lt;pgt;Đây chính là nền móng để xây dựng một lt;stronggt;Admin Dashboard thực tếlt;/stronggt;.lt;/pgt;lt;hr /gt;lt;h2gt;📚 Sau Bài 28lt;/h2gt;lt;pgt;Blog CMS hiện đã có:lt;/pgt;lt;pregt;lt;code class=quot;language-textquot;gt; BLOG CMS
│
┌────────────┼────────────┐
↓ ↓ ↓
Categories Posts Users
│ │ │
↓ ↓ ↓
CRUD CRUD CRUD
│
┌─────────┼─────────┐
↓ ↓ ↓
Role Lock Password
lt;/codegt;lt;/pregt;lt;pgt;lt;stronggt;Bài 29 — Search DataTableslt;/stronggt; sẽ nâng cấp hệ thống danh sách hiện tại, đặc biệt là Users và Posts, với:lt;/pgt;lt;ulgt;lt;ligt;lt;pgt;🔎 Search nâng caolt;/pgt;lt;/ligt;lt;ligt;lt;pgt;↕️ Sortlt;/pgt;lt;/ligt;lt;ligt;lt;pgt;🏷️ Filterlt;/pgt;lt;/ligt;lt;ligt;lt;pgt;📄 Paginationlt;/pgt;lt;/ligt;lt;ligt;lt;pgt;📊 DataTables.netlt;/pgt;lt;/ligt;lt;ligt;lt;pgt;⚡ Tương tác bảng dữ liệu chuyên nghiệp hơnlt;/pgt;lt;/ligt;lt;/ulgt;