簡體   English   中英

在查詢中不起作用Laravel

[英]whereIn query not working laravel

您能幫我查詢什么問題嗎? 在第二個變量中,我不能簡單地使用whereIn方法將其輸出到刀片中。 我應該在$ filterrecord變量中添加另一個位置,但是經過檢查,它甚至都沒有通過。

控制者

$querys 
    = RecordHistory::select(DB::raw('profile_id, max(effective_date) as date, membership_type'))
        ->groupBy('profile_id')         
        ->orderBy('date', 'desc')
        ->get();

$filterrecord
    = RecordHistory::select('profile_id', 'membership_type')
    ->whereIn('profile_id', $querys)        
    ->get();

數據庫查詢返回集合並且whereIn接受數組,因此首先將集合更改為數組,您可以使用以下代碼並實現它

 $filterrecord
        = RecordHistory::select('profile_id', 'membership_type')
        ->whereIn('profile_id', $querys->toArray())        
        ->get();

如果在查詢中使用whereInwhereIn逗號分隔傳遞多個ID

    $querys 
        = RecordHistory::select(DB::raw('profile_id, max(effective_date) as date, membership_type'))
            ->groupBy('profile_id')         
            ->orderBy('date', 'desc')
            ->get();
    $ids = array(); 
     foreach($querys as $item){
       $ids[]=$item->profile_id;
     }       
    $filterrecord
        = RecordHistory::select('profile_id', 'membership_type')
        ->whereIn('profile_id', $ids)        
        ->get();

whereIn的參數2th是數組。但是您的變量“ $ querys”是一個集合對象。將其轉換為列表profile_id的數組。

$ids = array();
foreach($querys as $item){
  $ids[]=$item->profile_id;
}

要么

$ids = array(); 
foreach($querys as $item){
  $ids[]=$item['profile_id'];
}

並像這樣使用它:

->whereIn('profile_id', $ids)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM