溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Laravel關(guān)聯(lián)模型中如何過濾結(jié)果為空的結(jié)果集

發(fā)布時(shí)間:2021-09-10 09:32:58 來源:億速云 閱讀:157 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Laravel關(guān)聯(lián)模型中如何過濾結(jié)果為空的結(jié)果集,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

首先看代碼:

$userCoupons = UserCoupons::with(['coupon' => function($query) use($groupId){
 return $query->select('id', 'group_id', 'cover', 'group_number', 'group_cover')->where([
   'group_id' => $groupId,
 ]);
}])
// 更多查詢省略...

數(shù)據(jù)結(jié)構(gòu)是三張表用戶優(yōu)惠券表(user_coupons)、優(yōu)惠券表(coupons),商家表(corps),組優(yōu)惠券表(group_coupons) (為了方便查看,后兩項(xiàng)已去除)

這里我本意想用模型關(guān)聯(lián)查出用戶優(yōu)惠券中屬于給定組gourpId的所有數(shù)據(jù)(如果為空該條數(shù)據(jù)就不返回)。

但有些結(jié)果不是我想要的:

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為空的數(shù)據(jù)
}

記錄中有的coupon有記錄,有的為空。想想也是,with只是用sql的in()實(shí)現(xiàn)的所謂預(yù)加載。無論怎樣主user_coupons的數(shù)據(jù)都是會(huì)列出的。

它會(huì)有兩條sql查詢,第一條查主數(shù)據(jù),第二條查關(guān)聯(lián),這里第二條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

如果第二條為空,主記錄的關(guān)聯(lián)字段就是NULL。

后來看到了Laravel關(guān)聯(lián)的模型的has()方法,has()是基于存在的關(guān)聯(lián)查詢,下面我們用whereHas()(一樣作用,只是更高級(jí),方便寫條件)

這里我們思想是把判斷有沒有優(yōu)惠券數(shù)據(jù)也放在第一次查詢邏輯中,所以才能實(shí)現(xiàn)篩選空記錄。

加上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)

這里實(shí)際上是用exists()篩選存在的記錄。然后走下一步的with()查詢,因?yàn)榇藭r(shí)都篩選一遍了,所以with可以去掉條件。

顯然區(qū)分這兩個(gè)的作用很重要,尤其是在列表中,不用特意去篩選為空的數(shù)據(jù),而且好做分頁(yè)。

以上是“Laravel關(guān)聯(lián)模型中如何過濾結(jié)果為空的結(jié)果集”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI