thinkphp6 belongsToMany多对多带where关联查询

tp6 手册 多对多关联 · ThinkPHP6.0完全开发手册 · 看云 ThinkPHP`6.0`基于精简核心和统一用法两大原则在`5.1`的基础上对底层架构做了进一步的优化改进,并更加规范化。icon-default.png?t=M3C8https://www.kancloud.cn/manual/thinkphp6_0/1037603

coupon_cate 分类表

coupon_brand 品牌表

coupon_brand_cate_relation 分类和品牌 =》 多对多关系表

代码如下

/**
     * @descripe 关联关系-品牌
     */
    public function couponBrand(){

        return $this->belongsToMany(CouponBrand::class,'coupon_brand_cate_relation','brand_id','cate_id');

    }

多对多关联查询 带where其他条件

$data = CouponCate::query()->where('coupon_cate.status', CouponCate::STATUS_ACTIVE)
            ->with(['couponBrand' => function($query) {
                // $query->getQuery() 需要取出原先的查询条件在拼上新的where条件
                $query->getQuery()->where('coupon_brand.status', CouponBrand::STATUS_ACTIVE)->order('sort desc');         
            }])
            
           ->select()->toArray();

with输出的sql语句 

SELECT `coupon_brand`.*,`pivot`.`cate_id` AS `pivot__cate_id`,`pivot`.`brand_id` AS `pivot__brand_id` FROM `coupon_brand` INNER JOIN `coupon_brand_cate_relation` `pivot` ON `pivot`.`brand_id`=`coupon_brand`.`id` WHERE `coupon_brand`.`status` = '1' AND `pivot`.`cate_id` IN ('1','2','3','4','5','6','7','8','9') ORDER BY `sort` DESC 

闭包查询 中的dump($query->getQuery());

关键点 $query->getQuery() 取出原先的查询条件 在拼上where的其他条件

注意以下这种写法会覆盖掉原先的where查询条件

$data = CouponCate::query()->where('coupon_cate.status', CouponCate::STATUS_ACTIVE)
           ->with(['couponBrand' => function($query){
               //todo 覆盖where条件,导致多对多查询无效,返回的是最后一条数据
               $query->where('coupon_brand.status', CouponBrand::STATUS_ACTIVE);
           }])

输出的sql语句 SELECT `coupon_brand`.*,`pivot`.`cate_id` AS `pivot__cate_id`,`pivot`.`brand_id` AS `pivot__brand_id` FROM `coupon_brand` INNER JOIN `coupon_brand_cate_relation` `pivot` ON `pivot`.`brand_id`=`coupon_brand`.`id` WHERE `pivot`.`cate_id` = '9' AND `coupon_brand`.`status` = '1'

学习笔记,如有侵权,请联系删除!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ThinkPHP6中,实现一对多关联查询的方法是使用hasMany模式。具体设置方式如下: 1. 在主表的模型中使用hasMany方法进行关联设置: ``` public function 附表名() { return $this->hasMany(附表模型::class, '外键字段', '主键字段'); } ``` 例如: ``` public function profile() { return $this->hasMany(Profile::class, 'user_id', 'id'); } ``` 2. 进行一对多关联查询: ``` $user = UserModel::find(主表记录的id); return json($user->附表关联方法名); ``` 例如: ``` $user = UserModel::find(19); return json($user->profile); ``` 3. 进行数据筛选查询: ``` $user->附表关联方法名->where('条件字段', '条件操作符', '条件值'); ``` 例如: ``` $user->profile->where('id', '>=', 10); ``` 4. 使用has()方法查询满足条件的主表记录: ``` UserModel::has('附表关联方法名', '条件操作符', '条件值')->select(); ``` 例如: ``` UserModel::has('profile', '>=', 2)->select(); ``` 5. 使用together()方法,在删除主表记录时,同时删除关联的附表记录: ``` $user = UserModel::with('附表关联方法名')->find(主表记录的id); $user->together(['附表关联方法名'])->delete(); ``` 例如: ``` $user = UserModel::with('profile')->find(22); $user->together(['profile'])->delete(); ``` 以上就是在ThinkPHP6中实现一对多关联查询的方法。希望对你有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yunxixiao

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值