简体   繁体   English

在查询中不起作用Laravel

[英]whereIn query not working laravel

Could you help what's the problem with my query. 您能帮我查询什么问题吗? In the second variable, I can't simply output the thing in my blade it using whereIn method. 在第二个变量中,我不能简单地使用whereIn方法将其输出到刀片中。 I'm supposed to add another where in the $filterrecord variable, but upon checking, it's not even going through. 我应该在$ filterrecord变量中添加另一个位置,但是经过检查,它甚至都没有通过。

Controller 控制者

$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();

Database query returns collection and whereIn accepts array thus first change the collection to an array, you can use the below code and achieve it 数据库查询返回集合并且whereIn接受数组,因此首先将集合更改为数组,您可以使用以下代码并实现它

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

If you use whereIn in query then you pass multiple ids by comma separated 如果在查询中使用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();

Parameter 2th of whereIn is array.But your variable "$querys" is a collection object.convert this to array of list profile_id. whereIn的参数2th是数组。但是您的变量“ $ querys”是一个集合对象。将其转换为列表profile_id的数组。

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

or 要么

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

and use it like: 并像这样使用它:

->whereIn('profile_id', $ids)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM