## 1.路由
routes/web.php
```
<?php
Route::namespace('Admin')->group(function () {
Route::group(['middleware' => 'login'], function () {
Route::match(['GET','POST'], '/adminedit/{id?}', 'Admin@edit');
});
```
## 2.管理员编辑
admin/admin/edit.balde.php
![](https://i.vgy.me/Weuruj.png)
```html
@include('admin.public.head') //引入头部公共文件
<div class="main-container container-fluid">
<div class="page-container">
@include('admin.public.left') //引入左侧公共文件
<div class="page-content">
<div class="page-breadcrumbs">
<ul class="breadcrumb">
<li class="active">
<i class="fa fa-home"></i> 管理员管理
</li>
<li>
管理员编辑
</li>
</ul>
</div>
<div class="page-body">
<div class="row">
<div class="col-xs-12">
<div class="widget radius-bordered">
<div class="widget-header bordered-bottom bordered-themeprimary">
<span class="widget-caption">管理员编辑</span>
</div>
<div class="widget-body">
<form class="form-horizontal">
<input type="hidden" name="id" value="{{$adminsInfo->id}}">
<div class="form-group">
<label for="username" class="col-sm-2 control-label no-padding-right">管理员账号</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="username" placeholder="请输入管理员账号" disabled="" value="{{$adminsInfo->username}}" />
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label no-padding-right">管理员原密码</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="old-password" placeholder="请输入原密码" />
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label no-padding-right">管理员新密码</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="new-password" placeholder="请输入新密码" />
</div>
</div>
<div class="form-group">
<label for="nickname" class="col-sm-2 control-label no-padding-right">管理员昵称</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="nickname" placeholder="请输入管理昵称" value="{{$adminsInfo->nickname}}" />
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label no-padding-right">邮箱</label>
<div class="col-sm-6">
<input type="email" class="form-control" name="email" disabled placeholder="请输入邮箱" value="{{$adminsInfo->email}}" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="update" class="btn btn-primary">更新</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@include('admin.public._js') //引入底部公共文件
<script>
$('#update').click(function () {
$.ajax({
url:"{{url('edit')}}",
type:'post',
data:$('form').serialize(),
dataType:'json',
success:function (data) {
if (data.code == 1){
layer.msg(data.msg,{
icon:6,
time:2000,
},function () {
location.href=data.url;
});
}else {
layer.open({
title:'修改失败',
content:data.msg,
icon:5
});
}
}
});
return false;
});
</script>
</body>
<!-- /Body -->
</html>
```
## 3.控制器
Controller/admin/admin.php(edit操作方法)
```php
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Admin as AdminModel;
use App\Http\Controllers\Controller;
class Admin extends Controller
{
// 编辑管理员
public function edit()
{
if (request()->isMethod('post')){
$data=request()->only(['id','old-password','new-password','nickname']);
$res=(new AdminModel)->edit($data);
if ($res == 1){
$msg=[
'code'=>1,
'msg'=>'修改成功',
'url'=>url('admins')
];
}else{
$msg=[
'code'=>0,
'msg'=>$res,
];
}
return $msg;
}
$adminsInfo=AdminModel::find(request('id'));
$viewData=[
'adminsInfo'=>$adminsInfo
];
return view('admin.admin.edit',$viewData);
}
}
```
## 4.模型
Models/Admin.php(edit方法)
```php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model
{
use SoftDeletes;
protected $fillable=['username','password','nickname','email'];
protected $dateFormat='U';
protected $dates=['created_at','updated_at','deleted_at'];
// 修改管理员
public function edit($data)
{
$rule=[
'old-password'=>'bail|required',
'new-password'=>'required',
'nickname'=>'required',
];
$msg=[
'old-password.required'=>'原密码不能为空',
'new-password.required'=>'新密码不能为空',
'nickname.required'=>'昵称不能为空'
];
$validate=Validator::make($data,$rule,$msg);
if ($validate->fails()){
return $validate->errors()->first();
}
$res=$this->find($data['id']);
if ($data['old-password'] != $res->password){
return '原密码不正确';
}
$res->password=$data['new-password'];
$res->nickname=$data['nickname'];
$save=$res->save();
if ($save){
return 1;
}else{
return '修改失败';
}
}
}
```