with关联查询的使用,hasOne/hasMany/belongsTo

TP5中 经常要连表查用的是with,with熟练使用可以快速完成编程。

with在controller 中的常见用法:

$data = \app\admin\model\patients\Patients::where("id", $id)
    ->with(["room", "bed", "clinical"])
    ->find();

 同时在model中的Patients.php需要添加:

    public function clinical()
    {
        return $this->hasOne('Clinical', "id", "clinical_id");
    }

    public function room()
    {
        return $this->hasOne('app\admin\model\space\Info', "id", "space_id");
    }

    public function bed()
    {
        return $this->hasOne('app\admin\model\space\Bed', "id", "bed_id");
    }

当然也可以多个表联查,例如:

$data = \app\admin\model\Document::order('type')
    ->where($where)
    ->where('status',1)
    ->with(['file.user','user'])
    ->paginate($pagesize);

在model中的Document.php页面中添加:

    public function file()
    {
        return $this->hasOne(File::class, 'id', 'file_id');
    }
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->field("id,username,nickname,prevtime,logintime,jointime");
    }

在model中的File.php页面中添加:

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->field("id,username,nickname,prevtime,logintime,jointime");
    }

常见的关联查询的使用:hasOne/hasMany/belongsTo

hasOne: 一对一关联

数据之间的关联一对一。例如:一个file表里面的上传人对应一个user表中的user信息。

hasOne('别的表模型名','别的表外键名','本表主键名',['模型别名定义'],'join类型');

    public function user(){
        return $this->hasOne(User::class, 'id', 'create_id')->field("id,username");
    }

hasMany: 一对多关联

数据之间的关联一对多。例如:一个user表里面的上传文件对应file表中的多个file。 

 withcount用法:

$rows = \app\admin\model\UserGroup::where($map)
    ->with(['department','user'])
    ->withCount('linkUser')
    ->paginate($pageSize);
    public function department(){
        return $this->belongsTo(Department::class,"department_id","id")->field('id,name');
    }

    public function linkUser(){
        return $this->hasMany(UserLinkGroup::class,"gid","id");
    }

    public function user(){
        return $this->hasOne(User::class, 'id', 'create_id')->field("id,username");
    }

belongsTo:属于

当前方法的模型对应的数据表,属于另一个模型。相当于多对一。

belongsTo('别的表模型名','本表外键名','本表关联表主键名',['模型别名定义'],'join类型');

    public function department(){
        return $this->belongsTo(Department::class,"department_id","id")->field('id,name');
    }

深入写法:

添加一些条件语句查询:

        $data = CategoryModel::with(['files' => function($q) use ($unVisibleFileIds){
            $q->where('id','not in', $unVisibleFileIds)->where('status','1')->where('pid',0);
        }])->where($map)->select();
        $need_total = WorkOrder::whereIn("ordertype", ["1", "2", "3"])->where($map)->where(function ($query) use ($startTimeTamps, $endTimeTamps) {
            $query->whereOr(function ($q) use ($startTimeTamps, $endTimeTamps) {
                $q->whereBetween("hope_endtime", [$startTimeTamps, $endTimeTamps]);
            })
                ->whereOr("complete_time", ">", $endTimeTamps)
                ->whereOr(function ($q) {
                    $q->whereNotIn("status", ["6", "8"]);
                });
        })->count();

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值