thinkphp6 模型 使用实例

拥有回忆 提交于 2019-12-10 10:12:57

thinkphp6 模型

例子

  1. 关联模型
  2. 关联输出,分页查询
    /**
     * 定义关联模型方法
     * @return \think\model\relation\HasOne
     */
    public function authorInfo(){
        // 一对一
        return $this->hasOne(User::class,'id','author');
    }

    /**
     * 列表查找
     * @param int $type 类型
     * @param int $page 当前页面
     * @param int $limit 每页面数量
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function GetListAll(int $type = 1, int $page = 1, int $limit = 100){
        // 关联输出
        return $this::where('type',$type)
            ->with('authorInfo') // 载入关联模式方法,方法名就是字段名称
            ->visible(['authorInfo'=>['id','nick_name']]) //显示需要的数据
            ->field('id,type,title,author,cover,likes,update_time')
            // 分页查询
            ->paginate([
                'list_rows'=>$limit,
                'page'=>$page
            ])->toArray();
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!