thinkphp6 模型 使用实例

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();
    }
相关文章
相关标签/搜索