Laravel學(xué)習(xí)筆記-with()和whereHas()

需求:Laravel關(guān)聯(lián)模型中過(guò)濾結(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]);
}])
// 更多查詢(xún)省略...

數(shù)據(jù)結(jié)構(gòu)是三張表用戶(hù)優(yōu)惠券表(user_coupons)、優(yōu)惠券表(coupons),商家表(corps),組優(yōu)惠券表(group_coupons)

需求:用模型關(guān)聯(lián)查出用戶(hù)優(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ù)加載。

無(wú)論怎樣主user_coupons的數(shù)據(jù)都是會(huì)列出的。

它會(huì)有兩條sql查詢(xún),第一條查主數(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。

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

這里我們思想是把判斷有沒(méi)有優(yōu)惠券數(shù)據(jù)也放在第一次查詢(xún)邏輯中,所以才能實(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()查詢(xún),因?yàn)榇藭r(shí)都篩選一遍了,所以with可以去掉條件。

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容