1.模型修改
// 表名
protected $name = 'agent';
// 自动写入时间戳字段
protected $autoWriteTimestamp = false;
// 定义时间戳字段名
protected $createTime = false;
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
];
public function user()
{
return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
//level_id 关联id
//id 主键id
public function level()
{
return $this->belongsTo('AgentLevel', 'level_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
2.控制器
public function index()
{
//当前是否为关联查询
$this->relationSearch = true; //开启关联
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['user', 'level']) //模型中的联查表
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->getRelation('user')->visible(['nickname']);//字段过滤
$row->getRelation('level')->visible(['name']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
3.js文件 这里仅为示例,代码不完整,自己能找到地方放进去
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'user_id', title: __('User_id')},
{field: 'user_id', title: __('User_id')},
{field: 'real_name', title: __('Real_name'), operate: 'LIKE'},
{field: 'id_card', title: __('Id_card'), operate: 'LIKE'},