Laravel关联模型中过滤结果为空的结果集(has和with区别)
2018-10-18 11:32
2931 查看
首先看代码:
$userCoupons = UserCoupons::with(['coupon' => function($query) use($groupId){ return $query->select('id', 'group_id', 'cover', 'group_number', 'group_cover')->where([ 'group_id' => $groupId, ]); }]) // 更多查询省略...
数据结构是三张表用户优惠券表(user_coupons)、优惠券表(coupons),商家表(corps),组优惠券表(group_coupons) (为了方便查看,后两项已去除)
这里我本意想用模型关联查出用户优惠券中属于给定组gourpId的所有数据(如果为空该条数据就不返回)。
但有些结果不是我想要的:
array(20) { ["id"]=> int(6) ["user_id"]=> int(1) ["corp_id"]=> int(1) ["coupon_id"]=> int(4) ["obtain_time"]=> int(1539739569) ["receive_time"]=> int(1539739569) ["status"]=> int(1) ["expires_time"]=> int(1540603569) ["is_selling"]=> int(0) ["from_id"]=> int(0) ["sell_type"]=> int(0) ["sell_time"]=> int(0) ["sell_user_id"]=> int(0) ["is_compose"]=> int(0) ["group_cover"]=> string(0) "" ["is_delete"]=> int(0) ["score"]=> int(100) ["created_at"]=> NULL ["updated_at"]=> NULL ["coupon"]=> NULL // 注意返回了coupons为空的数据 }
记录中有的coupon有记录,有的为空。想想也是,with只是用sql的in()实现的所谓预加载。无论怎样主user_coupons的数据都是会列出的。
它会有两条sql查询,第一条查主数据,第二条查关联,这里第二条sql如下:
select `id`, `group_id`, `cover`, `group_number`, `group_cover` from `youquan_coupons` where `youquan_coupons`.`id` in (1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14) and (`group_id` = 1) and `youquan_coupons`.`deleted_at` is null
如果第二条为空,主记录的关联字段就是NULL。
后来看到了Laravel关联的模型的has()方法,has()是基于存在的关联查询,下面我们用whereHas()(一样作用,只是更高级,方便写条件)
这里我们思想是把判断有没有优惠券数据也放在第一次查询逻辑中,所以才能实现筛选空记录。
加上whereHas()后的代码如下
$userCoupons = UserCoupons::whereHas('coupon', function($query) use($groupId){ return $query->select('id', 'group_id', 'cover', 'group_number', 'group_cover')->where([ 'group_id' => $groupId, ]); })->with(['coupon' => function($query) use($groupId){ return $query->select('id', 'group_id', 'cover', 'group_number', 'group_cover'); }])-> // ...
看下最终的SQL:
select * from `youquan_user_coupons` where exists (select `id`, `group_id`, `cover`, `group_number`, `group_cover` from `youquan_coupons` where `youquan_user_coupons`.`coupon_id` = `youquan_coupons`.`id` and (`group_ids` = 1) and `youquan_coupons`.`deleted_at` is null) and (`status` = 1 and `user_id` = 1)
这里实际上是用exists()筛选存在的记录。然后走下一步的with()查询,因为此时都筛选一遍了,所以with可以去掉条件。
显然区分这两个的作用很重要,尤其是在列表中,不用特意去筛选为空的数据,而且好做分页。
总结
以上所述是小编给大家介绍的Laravel关联模型中过滤结果为空的结果集(has和with区别),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
相关文章推荐
- laravel 模型关联 的简单使用方法
- 深入理解 Laravel Eloquent(三)——模型间关系(关联)
- 关联模型主被动关联的区别
- laravel 5.5 -- Eloquent 模型关联
- Laravel 模型关联attach,save,sync方法参数类型验证
- Laravel 5 - Trait method can has not been applied, because there are collisions with other trai
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第三章:搜索、高级过滤和视图模型
- 一对一关联模型,HAS_ONE
- Laravel Trait method broker has not been applied, because there are collisions with other trait meth
- Yii2中多表关联查询(hasOne、hasMany、join、joinwith)
- 深入理解 Laravel Eloquent(三)——模型间关系(关联) 在本篇文章中,我将跟大家一起学习 Eloquent 中最复杂也是最难理解的部分——模型间关系。官方英文文档中叫 Relatio
- laravel eloquent 模型关联
- 深入理解 Laravel Eloquent(三)——模型间关系(关联)
- 深入理解 Laravel Eloquent(三)——模型间关系(关联)
- Laravel5.2之模型关联预加载
- union 与union all都是合并结果集,不过它们之间是有区别的,union会自动过滤重复的记录值,union all则不会过滤
- SQL2000视图中关联与where子句中条件判断的结果有所区别
- 对group by的结果集进行分组过滤 where 与 having 用法的区别
- laravel框架hasone与belongsto的with用法
- Hive&SqlServerql:inner join on条件中如果两边都是空值的情况下,关联结果中会把数据给过滤掉。