例atten(关注表)中的userId(用户ID)和attenUserId(被关注用户ID),需要通过关联user(用户表)的id,获取user(用户表)的username(呢称)
因为fastadmin的在线命令只能关联同一个表一次性,所以只能先关联一次,然后进入代码中进行修改
model中修改,原有
public function user() { return $this->belongsTo('User', 'attenUserId', 'id', [], 'LEFT')->setEagerlyType(0); }
添加多一个
public function user1() { return $this->belongsTo('User', 'userId', 'id', [], 'LEFT')->setEagerlyType(0); }
controller中修改,原代码
$list = $this->model ->with(['user']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->getRelation('user')->visible(['userName']); }
修改为
$list = $this->model ->with(['user','user1']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->getRelation('user')->visible(['userName']); $row->getRelation('user1')->visible(['userName']); }
在lang中添加
'User1.username' => '用户呢称'
在js中添加
{field: 'user1.userName', title: __('User1.username'), operate: 'LIKE'},
完成