## 1.路由
routes/web.php
```
~~~
Route::namespace('Admin')->group(function () {
Route::group(['middleware' => 'login'], function () {
Route::match(['GET','POST'], '/Add', 'Admin@Add');
});
});
```
## 2.管理员添加页面
admin/admin/add.balde.php
![](https://i.vgy.me/cjpGFr.png)
```html
@include('admin.public.head') //引入公共头部文件
<div class="main-container container-fluid">
<!-- Page Container -->
<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">
<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="请输入管理员账号" />
</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="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="请输入管理员账号" />
</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" placeholder="请输入邮箱" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="adminadd" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@include('admin.public._js') //引入公共底部文件
<script>
$('#adminadd').click(function () {
$.ajax({
url:"{{url('Add')}}",
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>
</html>
```
## 3.控制器
Controller/admin/admin.php(list操作方法)
```php
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Admin as AdminModel;
use App\Http\Controllers\Controller;
class Admin extends Controller
{
// 添加管理员
public function Add()
{
if (request()->isMethod('post')){
$data=request()->only(['username','password','nickname','email']);
$res=(new AdminModel)->add($data);
if ($res == 1){
$msg=[
'code'=>1,
'msg'=>'添加管理员成功',
'url'=>url('admins')
];
}else{
$msg=[
'code'=>0,
'msg'=>$res,
];
}
return $msg;
}
return view('admin.admin.add');
}
```
## 4.模型
Models/Admin.php(list方法)
```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 add($data)
{
$rule=[ //验证规则
'username'=>'bail|required|unique:admins',
'password'=>'required',
'nickname'=>'required',
'email'=>'required|email|unique:admins',
];
$msg=[ //提示错误信息
'username.required'=>'请输入账户',
'username.unique'=>'此账户已存在',
'password.required'=>'请输入密码',
'nickname.required'=>'请输入昵称',
'email.required'=>'请输入邮箱',
'email.email'=>'邮箱格式不正确',
'email.unique'=>'此邮箱已注册!',
];
$validate=Validator::make($data,$rule,$msg);
if ($validate->fails()){
return $validate->errors()->first();
}
$res=$this->create($data);
if ($res){
return 1;
}else{
return '添加管理员失败';
}
}
}
```