Query子句有两个whereIn

时间:2017-04-14 08:46:16

标签: laravel

我尝试运行此查询,一切都很好,直到:

  

- > whereIn('statut_licence_id',['1','4'])

这第二个whereIn似乎不起作用,它显示我只有statut_licence_id = 1而不是4,有人现在如何解决问题?

        $licencies = Licencies::whereIn('structure_id', $structures->pluck('id'))->whereIn('type_licence_id', ['1', '2', '3', '4', '5'])
            ->whereIn('statut_licence_id', ['1' , '4'])
            ->where('valid_licence_id', '1')
            ->where('lb_assurance_etat' ,'=' , 'Assurée')
            ->orderBy('created_at', 'DESC')
            ->paginate(10);
问题解决了:

$licencies = Licencies::whereIn('structure_id', $structures->pluck('id'))->whereIn('type_licence_id', ['1', '2', '3', '4', '5']) ->where(function ($query) {
            $query->whereIn('statut_licence_id', ['1' , '4'])
            ->where('valid_licence_id', '1'); })
            ->orderBy('created_at', 'DESC')
            ->paginate(10);

2 个答案:

答案 0 :(得分:2)

尝试使用闭包。

$licencies = Licencies::whereIn('structure_id', $structures->pluck('id'))->whereIn(function ($query){
             $query->whereIn('type_licence_id', ['1', '2', '3', '4', '5'])
            ->whereIn('statut_licence_id', ['1' , '4']);
        })
        ->where('valid_licence_id', '1')
        ->where('lb_assurance_etat' ,'=' , 'Assurée')
        ->orderBy('created_at', 'DESC')
        ->paginate(10);

答案 1 :(得分:1)

您可以按照以下步骤检查查询语句:

首先,在声明之前:

DB::enableQueryLog();

其次,使用dd在语句后打印你的sql:

dd(DB::getQueryLog());

最后,将sql复制并粘贴到您的数据库客户端中,您将得到您想要的内容。而且你不需要再问任何有相似之处的人。

相关问题