第一步,在用户表里面的index.js文件里面写入渠道表的name字段

qudao : 表名称
name  :字段名称

{field: 'qudao.name', title: __('渠道')}

第二步,在用户的模型里面新建一个方法

Qudao:表名称
qudao_id :用户表里面的渠道id字段

id:用户表里面的qudao_id 等于 Qudao表中的 id

public function qudao()
       {
        return $this->belongsTo('Qudao', 'qudao_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

Ps:如果关联的表模型不在当前模型的同一目录,跨目录的话可以用下面这个方法

    public function feiyong()
    
    {
         
        return $this->belongsTo('app\admin\model\wechat\fee\Category', 'fee_category', 'id', [], 'LEFT')->setEagerlyType(0);
    }

第三步:在用户的控制器里面加入with方法

这里的 qudao 是你创建的方法名字 

->with(['group','qudao']) 如果多个字段需要两表联查就用数组的方式

->with('qudao') 只有一个字段,就直接放进去就行了。

/**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $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(['group','qudao'])
                // ->with('qudao')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

这样子用户表中存储qudao_id的int类型,也可以用qudao表的name来进行搜索。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐