Skip to content
关注公众号,获取新课通知

管理员列表


控制器 app/controller/admin/Manager.php

php
public function index()
{
       $param = request()->param();
       $limit = intval(getValByKey('limit',$param,10));
       $keyword = getValByKey('keyword',$param,'');
       $where = [
       	[ 'username','like','%'.$keyword.'%' ]
       ];
       
       $totalCount = $this->M->where($where)->count();
       $list = $this->M->page($param['page'],$limit)
       		->where($where)
       		->with('role')
	        ->order([ 'id'=>'desc' ])
			->select()
			->hidden(['password']);
	$role = \app\model\Role::field(['id','name'])->select();
       return showSuccess([
       	'list'=>$list,
       	'totalCount'=>$totalCount,
       	'role'=>$role
       ]);
   }

自定义方法 app/common.php

php
// 获取数组指定key的value
function getValByKey($key,$arr,$default = false)
{
    return array_key_exists($key,$arr) ? $arr[$key] : $default;
}

模型关联

php
// app/model/Manager.php
// 用户属于哪个角色 反向一对多
public function role(){
    return $this->belongsTo('Role');
}
// app/model/Role.php
// 角色-管理员 一对多关系
public function managers(){
	return $this->hasMany('Manager');
}

路由 router/admin.php

php
Route::get('manager/:page','admin.Manager/index');

验证器 app/validate/admin/Manager.php

php
protected $scene = [
	...
	'index'=>['page'],
	...
];