1. Modify user information
1.1. Define routes
// Modify the user display
Route::get('user/edit/{id}'.'UserController@edit') -> name('admin.user.edit');
// Modify user processing
Route::put('user/edit/{id}'.'UserController@update') -> name('admin.user.edit');
Copy the code
1.2 controller
// Modify the user display
public function edit (int $id) {
$model = User::find($id);
return view('admin.user.edit', compact('model'));
}
// Modify user processing
public function update (Request $request.int $id) {
$model = User::find($id);
/ / the original password
$spass = $request -> get('spassword');
// The original password is ciphertext
$oldpass = $model -> password;
$bool = Hash::check($spass.$oldpass);
// dump($bool);
if ($bool) {
/ / modify
$data = $request -> only([
'username'.'password'.'phone'.'sex'.'email'
]);
if (!empty($data['password']) {$data['password'] = bcrypt($data['password']);
}
else {
unset($data['password']);
}
$model -> update($data);
return redirect(route('admin.user.index')) -> with('success'.'User modified successfully');
}
else {
return redirect(route('admin.user.edit'.$model)) -> withErrors(['error'= >'Old password is incorrect']); }}Copy the code
1.3. Modify the template
Views/admin/user/index. The blade. PHP:
@if($item -> deleted_at == null)
<span class="label label-primary radius">
<a href="{{route('admin.user.edit', $item)}}">Modify the</a>
</span>
@endif
Copy the code
在views/admin/user
newedit.blade.php
Template. The code is given in the following key code.
Ii. Key codes in this section
UserController. PHP:
// Background user management
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use Faker\Provider\Base;
use App\Models\User;
use Mail;
use Illuminate\Mail\Message;
// Verify that the inscription password is consistent with the encrypted password
use Hash;
class UserController extends BaseController
{
// List of users
public function index() {
// echo $this->pagesize;
// paging withTrashed() displays everything, including those that have been soft deleted
$data = User::orderBy('id'.'desc') -> withTrashed() -> paginate($this->pagesize);
return view('admin.user.index', compact('data'));
}
// Add user display
public function add() {
return view('admin.user.add');
}
// User add processing
public function create(Request $request) {
$this->validate($request[// verify uniqueness
'username'= >'required|unique:users,username'.// 'account' => 'required',
'phone'= >'phone'.'email'= >'required'.'password'= >'required|confirmed',]);// dump($request -> all());
// Get form data
$post = $request -> except(['_token'.'password_confirmation']);
$post['password'] = bcrypt($post['password']);
// Add a user to the database
$userModel = User::create($post);
// dump($userModel);
/ / password
$pwd = $post['password'];
// Send an email to the user (anonymous functions pass in external variables using use)
Mail::send('mail.useradd', compact('userModel'.'pwd'), function(Message $message) use ($userModel){
/ / sent to who
$message ->to($userModel -> email);
/ / theme
$message -> subject('Email notification for Account opening');
});
// Jump to the list page
return redirect(route('admin.user.index')) -> with('success'.'User added successfully');
}
// Delete the user operation
public function del(int $id) {
/ / soft delete
User::find($id) -> delete();
// Forcible Delete Indicates the actual deletion operation when soft deletion is configured
// User::find($id) -> forceDelete();
return ['status'= >0.'msg'= >'Deleted successfully'];
}
// Restore the user
public function restore(int $id) {
/ / reduction
User::onlyTrashed() -> where('id'.$id) -> restore();
return redirect(route('admin.user.index')) -> with('success'.'Restore successful');
}
// Delete all users
public function delall (Request $request) {
$ids = $request -> get('id');
User::destroy($ids);
return ['status'= >1.'msg'= >'All selections deleted successfully'];
}
// Modify the user display
public function edit (int $id) {
$model = User::find($id);
return view('admin.user.edit', compact('model'));
}
// Modify user processing
public function update (Request $request.int $id) {
$model = User::find($id);
/ / the original password
$spass = $request -> get('spassword');
// The original password is ciphertext
$oldpass = $model -> password;
$bool = Hash::check($spass.$oldpass);
// dump($bool);
if ($bool) {
/ / modify
$data = $request -> only([
'username'.'password'.'phone'.'sex'.'email'
]);
if (!empty($data['password']) {$data['password'] = bcrypt($data['password']);
}
else {
unset($data['password']);
}
$model -> update($data);
return redirect(route('admin.user.index')) -> with('success'.'User modified successfully');
}
else {
return redirect(route('admin.user.edit'.$model)) -> withErrors(['error'= >'Old password is incorrect']); }}}Copy the code
Edit. Blade. PHP:
<! --_meta separated as public template -->
<! DOCTYPEHTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="Width = device - width, initial - scale = 1, minimum - scale = 1.0, the maximum - scale = 1.0, user - scalable = no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="Bookmark" href="/favicon.ico" >
<link rel="Shortcut Icon" href="/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="/ admin/lib/Hui - iconfont / 1.0.8 / iconfont. CSS" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/style.css" />
<title>Example Modify user -h-ui.admin v3.1</title>
</head>
<body>
<article class="page-container">
<! -- Form validation -->
@include('admin.common.validate')
<form action="{{route('admin.user.edit', $model)}}" method="post" class="form form-horizontal" id="form-member-add">
<! Let the form simulate a PUT submission -->
@method('PUT')
@csrf
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>Name:</label>
<div class="formControls col-xs-8 col-sm-9">
<! Flash memory -- -- -- >
<input type="text" class="input-text" value="{{$model -> username}}" placeholder="Name" id="username" name="username">
</div>
</div>
<! <div class="row cl"> <label class="form-label col-xs-4 col-sm-3"><span class="c-red"> </label> <div class="formControls col-xs-8 col-sm-9"> <input type="text" class="input-text" value="{{old('account')}}" Placeholder = "account" id = "account" name = "account" > < / div > < / div > -- >
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>The original password:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="password" class="input-text" autocomplete="off" name="spassword" >
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3">Password:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="password" class="input-text" autocomplete="off" value="" placeholder="Password" id="password" name="password">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3">Confirm password:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="password" class="input-text" autocomplete="off" placeholder="Confirm new password" id="password_confirmation" name="password_confirmation">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>Gender:</label>
<div class="formControls col-xs-8 col-sm-9 skin-minimal">
<div class="radio-box">
<input name="sex" type="radio" id="sex-1" value="0" checked>
<label for="sex-1">male</label>
</div>
<div class="radio-box">
<input type="radio" id="sex-2" name="sex" value="1">
<label for="sex-2">female</label>
</div>
<! - < div class = "radio - box" > < input type = "radio" id = "sex - 3" name = "sex" > < label for = "sex - 3" > confidentiality < / label > < / div > -- >
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>Mobile phone:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" class="input-text" value="{{$model -> phone}}" placeholder="Mobile phone number" id="phone" name="phone">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>Email address:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" class="input-text" placeholder="@" value="{{$model -> email}}" name="email" id="email">
</div>
</div>
<div class="row cl">
<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
<input class="btn btn-primary radius" type="submit" value=" Modify the user ">
</div>
</div>
</form>
</article>
<! --_footer separated as a public template -->
<script type="text/javascript" src="/ admin/lib/jquery / 1.9.1 / jquery. Min. Js." "></script>
<script type="text/javascript" src="/ admin/lib/layer / 2.4 / layer. The js." "></script>
<script type="text/javascript" src="/admin/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="/admin/static/h-ui.admin/js/H-ui.admin.js"></script> <! --/_footer as a public template -->
<! -- Please write the business related script of this page below -->
<script type="text/javascript" src="/ admin/lib/My97DatePicker / 4.8 / WdatePicker js." "></script>
<script type="text/javascript" src="/ admin/lib/jquery validation / 1.14.0 / jquery. Validate. Js." "></script>
<script type="text/javascript" src="/ admin/lib/jquery validation / 1.14.0 / validate - the methods. The js." "></script>
<script type="text/javascript" src="/ admin/lib/jquery validation / 1.14.0 / messages_zh js." "></script>
<script type="text/javascript">
$(function(){$('.skin-minimal input').iCheck({
checkboxClass: 'icheckbox-blue'.radioClass: 'iradio-blue'.increaseArea: '20%'
});
$("#form-member-add").validate({
rules: {username: {required:true,},spassword: {
required: true,},// password: {
// required: true,
// },
password_confirmation: {
equalTo: '#password'
},
phone: {required:true.isMobile:true,},email: {required:true.email:true,}}}); });</script>
<! --/ Please write the business related script of this page at the top -->
</body>
</html>
Copy the code
If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.