tp6 模型关联,添加不存在字段,显示字段,获取器用法

//获取器,重新赋值藏品售卖状态---前台
    public function getSaleTypeAttr($value,$data){
        if ($data['num']<=$data['sale_num']){
            $value = '已售罄';
        }else{
            $value = '售卖中';
        }
        return $value;
    }

    //查询藏品--前台
    public function apiAllColl($pag,$pageSize){
        $res =$this->withJoin(['issuer','category'])
            ->where('collection.status',1)
            ->where('collection.type',1)
            ->where('collection.start_buy','<=',time())
            ->where('collection.start_buy','<>',0)
            ->order('collection.id','desc')
            ->page($pag,$pageSize)
            ->select()
            ->append(['sale_type'])
            ->visible(['id','plan_img','name','cre_name','num','faxing_name','price','like_num'])
            ->hidden(['issuer','category'])
            ->toArray();
        $total = $this->withJoin(['issuer','category'])
            ->where('collection.status',1)
            ->where('collection.type',1)
            ->where('collection.start_buy','<=',time())
            ->where('collection.start_buy','<>',0)
            ->order('collection.id','desc')
            ->count();
        $res_arr['total'] = $total;
        $res_arr['per_page'] = $pageSize;
        $res_arr['current_page'] = $pag;
        $res_arr['last_page'] = ceil($total/$pageSize);
        $res_arr['data'] = $res;
        return $res_arr;
    }

//关联分类表
    public function category()
    {
        return $this->hasOne(Category::class,'id','cate_id')->bind(['cre_name'=>'name']);
    }

    //关联发行方
    public function issuer(){
        return $this->hasOne(Issuer::class,'id','issue_id')->bind(['faxing_name'=>'name']);
    }

declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Collection extends Model
{
    protected $table = 'wy_collection';
    protected $pk = 'id';
    //设置自动类型
    protected $type = [
        'start_buy'=>'timestamp',
    ];

    //查询所有数据
    public function couColl(){
        return $this->count();
    }

    //查询藏品--后台
    public function allColl($pag,$pageSize,$where){
        $neCo = $this->withJoin(['category','issuer'])
            ->where('wy_collection.name','like', '%'.$where.'%')
            ->paginate(['list_rows' => $pageSize, 'page' => $pag])
            ->toArray();
        return $neCo;
    }

    //修改藏品
    public function updColl($param){
        $sql = $this->findOrEmpty($param['id']);
        if ($sql->isEmpty()){
            return false;
        }
        if ($sql['sale_num']>0){
            return false;
        }
        $sql = $sql->save($param);
        return $sql;
    }

    //关联分类表
    public function category()
    {
        return $this->hasOne(Category::class,'id','cate_id')->bind(['cre_name'=>'name']);
    }

    //关联发行方
    public function issuer(){
        return $this->hasOne(Issuer::class,'id','issue_id')->bind(['faxing_name'=>'name']);
    }

    //获取器,重新赋值藏品售卖状态---前台
    public function getSaleTypeAttr($value,$data){
        if ($data['num']<=$data['sale_num']){
            $value = '已售罄';
        }else{
            $value = '售卖中';
        }
        return $value;
    }

   //查询藏品--前台
    public function apiAllColl($pag,$pageSize){
        $res =$this->withJoin(['issuer','category'])
            ->where('collection.status',1)
            ->where('collection.type',1)
            ->where('collection.start_buy','<=',time())
            ->where('collection.start_buy','<>',0)
            ->order('collection.id','desc')
            ->page($pag,$pageSize)
            ->select()
            ->append(['sale_type'])
            ->visible(['id','plan_img','name','cre_name','num','faxing_name','price','like_num'])
            ->hidden(['issuer','category'])
            ->toArray();
        $total = $this->withJoin(['issuer','category'])
            ->where('collection.status',1)
            ->where('collection.type',1)
            ->where('collection.start_buy','<=',time())
            ->where('collection.start_buy','<>',0)
            ->order('collection.id','desc')
            ->count();
        $res_arr['total'] = $total;
        $res_arr['per_page'] = $pageSize;
        $res_arr['current_page'] = $pag;
        $res_arr['last_page'] = ceil($total/$pageSize);
        $res_arr['data'] = $res;
        return $res_arr;
    }
use GuzzleHttp\Client;
/**
     *  请求方法
     */
    public function apiRequest($api, $data){
        $headers = ["Content-Type" => "application/json","X-HTTP-Method-Override" => "post","Content-Length"  => strlen(json_encode($data))];
        $client = new Client();
        $response = $client->request('post', $api,['headers' => $headers,'body'=>json_encode($data)]);
        $result = json_decode($response->getBody()->getContents(),true);
        return $result;
    }

composer

"require": {
        "php": ">=7.2.5",
        "topthink/framework": "^6.0.0",
        "topthink/think-orm": "^2.0",
        "topthink/think-multi-app": "^1.0",
        "topthink/think-captcha": "^3.0",
        "guzzlehttp/guzzle": "^7.4",
        "firebase/php-jwt": "^6.2",
      "ext-json": "*",
        "topthink/think-annotation": "1.1.3",
        "topthink/think-view": "^1.0",
        "aliyuncs/oss-sdk-php": "^2.5",
        "simplito/elliptic-php": "^1.0",
        "dh2y/think-qrcode": "^2.0",
        "topthink/think-queue": "^3.0"
    },

你可能感兴趣的:(thinkphp6,javascript,jquery,开发语言)