Tinker cho phép bạn tương tác với toàn bộ ứng dụng Laravel của mình trên dòng lệnh, bao gồm Eloquent ORM, công việc, sự kiện, v.v. Để vào môi trường Tinker, hãy chạy lệnh tinker Artisan
php artisan tinker or php artisan ti
bằng cách sử dụng dòng lệnh tinker, chúng ta có thể tạo người dùng mới hoặc chèn dữ liệu mới vào cơ sở dữ liệu
chạy php artisan ti
hoặc bạn có thể sử dụng các phương pháp khác nhau để lưu trữ dữ liệu mới, hãy xem
________số 8_______
Chúng tôi đã tạo người dùng bảng quản trị, vai trò và quản lý quyền. Trong phần này, chúng tôi sẽ tạo một trang cập nhật tài khoản cho người dùng bảng quản trị
Trang cập nhật tài khoảnCác bước sau đây có liên quan để tạo trang cập nhật tài khoản Bảng điều khiển quản trị Laravel
- 1. Thêm tuyến đường
- 2. Thêm một chức năng trên bộ điều khiển
- 3. Tạo chế độ xem
- 4. Lưu thông tin tài khoản
- 5. Lưu mật khẩu
- 6. Thêm liên kết vào điều hướng
1. Thêm tuyến đường
Chúng tôi sẽ tạo một trang mới để cập nhật tài khoản. Thêm các tuyến bên dưới vào nhóm quản trị của chúng tôi
tuyến đường/quản trị viên. php
Route::get('edit-account-info', 'UserController@accountInfo')->name('admin.account.info');Route::post('edit-account-info', 'UserController@accountInfoStore')->name('admin.account.info.store');
Route::post('change-password', 'UserController@changePasswordStore')->name('admin.account.password.store');
Lộ trình đầu tiên là “chỉnh sửa tài khoản-thông tin” cho biểu mẫu cập nhật tài khoản. Hai cách khác để lưu thông tin tài khoản và mật khẩu
2. Thêm một chức năng trên bộ điều khiển
Thêm chức năng dưới đây vào UserController
ứng dụng/Http/Bộ điều khiển/Quản trị viên/Bộ điều khiển người dùng. php
public function accountInfo(){
$user = \Auth::user();
return view('admin.user.account_info', compact('user'));
}
3. Tạo chế độ xem
Tạo account_info. lưỡi. php bên trong thư mục resource/views/admin/user/. Trong chế độ xem, chúng tôi đã thêm hai biểu mẫu. Một để cập nhật thông tin tài khoản cơ bản (tên & email). Một cách khác là cập nhật mật khẩu tài khoản
tài nguyên/lượt xem/quản trị viên/người dùng/account_info. lưỡi. php
<x-app-layout><x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('My Account') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="px-6">
<h1 class="inline-block text-2xl sm:text-3xl font-extrabold text-slate-900 tracking-tight dark:text-slate-200 py-4 block sm:inline-block flex">{{ __('Account Info') }}</h1>
@if ($errors->account->any())
<ul class="mt-3 list-none list-inside text-sm text-red-400">
@foreach ($errors->account->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
@if(session()->has('account_message'))
<div class="mb-8 text-green-400 font-bold">
{{ session()->get('account_message') }}
</div>
@endif
</div>
<div class="w-full px-6 py-4 bg-white overflow-hidden">
<form method="POST" action="{{ route('admin.account.info.store') }}">
@csrf
<div class="py-2">
<label for="name" class="block font-medium text-sm text-gray-700{{$errors->account->has('name') ? ' text-red-400' : ''}}">{{ __('Name') }}</label>
<input id="name" class="rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 block mt-1 w-full{{$errors->account->has('name') ? ' border-red-400' : ''}}"
type="text"
name="name"
value="{{ old('name', $user->name) }}"
/>
</div>
<div class="py-2">
<label for="email" class="block font-medium text-sm text-gray-700{{$errors->account->has('email') ? ' text-red-400' : ''}}">{{ __('Email') }}</label>
<input id="email" class="rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 block mt-1 w-full{{$errors->account->has('email') ? ' border-red-400' : ''}}"
type="email"
name="email"
value="{{ old('email', $user->email) }}"
/>
</div>
<div class="flex justify-end mt-4">
<button type="submit" class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring ring-gray-300 disabled:opacity-25 transition ease-in-out duration-150">
{{ __('Update') }}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="py-3">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="px-6">
<h1 class="inline-block text-2xl sm:text-3xl font-extrabold text-slate-900 tracking-tight dark:text-slate-200 py-4 block sm:inline-block flex">{{ __('Change Password') }}</h1>
@if ($errors->password->any())
<ul class="mt-3 list-none list-inside text-sm text-red-400">
@foreach ($errors->password->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
@if(session()->has('password_message'))
<div class="mb-8 text-green-400 font-bold">
{{ session()->get('password_message') }}
</div>
@endif
</div>
<div class="w-full px-6 py-4 bg-white overflow-hidden">
<form method="POST" action="{{ route('admin.account.password.store') }}">
@csrf
<div class="py-2">
<label for="old_password" class="block font-medium text-sm text-gray-700{{$errors->password->has('old_password') ? ' text-red-400' : ''}}">{{ __('Old Password') }}</label>
<input id="old_password" class="rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 block mt-1 w-full{{$errors->password->has('old_password') ? ' border-red-400' : ''}}"
type="password"
name="old_password"
/>
</div>
<div class="py-2">
<label for="new_password" class="block font-medium text-sm text-gray-700{{$errors->password->has('new_password') ? ' text-red-400' : ''}}">{{ __('New Password') }}</label>
<input id="new_password" class="rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 block mt-1 w-full{{$errors->password->has('new_password') ? ' border-red-400' : ''}}"
type="password"
name="new_password"
/>
</div>
<div class="py-2">
<label for="confirm_password" class="block font-medium text-sm text-gray-700{{$errors->password->has('confirm_password') ? ' text-red-400' : ''}}">{{ __('Confirm password') }}</label>
<input id="confirm_password" class="rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 block mt-1 w-full{{$errors->password->has('confirm_password') ? ' border-red-400' : ''}}"
type="password"
name="confirm_password"
/>
</div>
<div class="flex justify-end mt-4">
<button type='submit' class='inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring ring-gray-300 disabled:opacity-25 transition ease-in-out duration-150'>
{{ __('Change Password') }}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</x-app-layout>
Mẫu cập nhật tài khoản
4. Lưu thông tin tài khoản
Tạo hàm accountInfoStore để lưu trữ chi tiết tài khoản người dùng
ứng dụng/Http/Bộ điều khiển/Quản trị viên/Bộ điều khiển người dùng. php
public function accountInfoStore(Request $request){
$request->validateWithBag('account', [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.\Auth::user()->id],
]);
$user = \Auth::user()->update($request->except(['_token']));
if ($user) {
$message = "Account updated successfully.";
} else {
$message = "Error while saving. Please try again.";
}
return redirect()->route('admin.account.info')->with('account_message', $message);
}
Chúng tôi đã sử dụng validateWithBag để phân tách thông báo lỗi khi xem. Laravel được gọi là. Bởi vì chúng tôi đã sử dụng hai biểu mẫu trên cùng một trang
5. Lưu mật khẩu
Hàm changePasswordStore a được sử dụng để lưu mật khẩu đã thay đổi của người dùng. Đã thêm xác thực $validator->after để xác thực mật khẩu hiện tại sau các quy tắc xác thực
ứng dụng/Http/Bộ điều khiển/Quản trị viên/Bộ điều khiển người dùng. php
public function changePasswordStore(Request $request){
$validator = \Validator::make($request->all(), [
'old_password' => ['required'],
'new_password' => ['required', Rules\Password::defaults()],
'confirm_password' => ['required', 'same:new_password', Rules\Password::defaults()],
]);
$validator->after(function ($validator) use ($request) {
if ($validator->failed()) return;
if (! Hash::check($request->input('old_password'), \Auth::user()->password)) {
$validator->errors()->add(
'old_password', 'Old password is incorrect.'
);
}
});
$validator->validateWithBag('password');
$user = \Auth::user()->update([
'password' => Hash::make($request->input('new_password')),
]);
if ($user) {
$message = "Password updated successfully.";
} else {
$message = "Error while saving. Please try again.";
}
return redirect()->route('admin.account.info')->with('password_message', $message);
}
6. Thêm liên kết vào điều hướng
Bước cuối cùng thêm liên kết Tài khoản của tôi trên public function accountInfo()
{
$user = \Auth::user();
return view('admin.user.account_info', compact('user'));
}0
tài nguyên/lượt xem/bố cục/điều hướng. lưỡi. php
<x-slot name="content"><!-- Authentication -->
+ <x-dropdown-link :href="route('admin.account.info')" :active="request()->routeIs('admin.account.info')">
+ {{ __('My Account') }}
+ </x-dropdown-link>
<form method="POST" action="{{ route('logout') }}">
@csrf
@@ -94,6 +97,9 @@
</div>
<div class="mt-3 space-y-1">
+ <x-responsive-nav-link :href="route('admin.account.info')" :active="request()->routeIs('admin.account.info')">
+ {{ __('My Account') }}
+ </x-responsive-nav-link>
<!-- Authentication -->
<form method="POST" action="{{ route('logout') }}">
@csrf
Chúng tôi đã tạo thành công trang cập nhật tài khoản cho bảng quản trị của chúng tôi
Bảng quản trị Laravel có sẵn tại https. //github. com/balajidharma/basic-laravel-admin-panel. Cài đặt bảng quản trị và chia sẻ phản hồi của bạn