Laravel 5复杂AND WHERE查询未按预期工作


Laravel 5 complex AND WHERE query not working as expected

我对以下代码有问题:

            $clients_counter = 0;
            $cost = 0;
            $query = DB::table('clientes')->where('recibe_sms', '=', '1')
            ->where(function($q)
            {
                $q->orWhere('movil_1',      '<>', '')
                  ->orWhere('movil_2',      '<>', '')
                  ->orWhere('otro_movil',   '<>', '');
            });
            $with_moto          = (Input::has('moto')) ? 1 : 0;
            $with_coche         = (Input::has('coche')) ? 1 : 0;
            $with_camion        = (Input::has('camion')) ? 1 : 0;
            $with_autobus       = (Input::has('autobus')) ? 1 : 0;
            $with_tractor       = (Input::has('tractor')) ? 1 : 0;
            $with_maquinaria    = (Input::has('maquinaria')) ? 1 : 0;
            $with_furgoneta     = (Input::has('furgoneta')) ? 1 : 0;
            $with_taxi          = (Input::has('taxi')) ? 1 : 0;

            $query = $query->where(function($q) use ($with_moto,$with_coche,$with_camion,$with_autobus,$with_tractor,$with_maquinaria,$with_furgoneta,$with_taxi)
            {
                $q->where('moto',       '=', $with_moto)
                  ->where('coche',      '=', $with_coche)
                  ->where('camion',     '=', $with_camion)
                  ->where('autobus',    '=', $with_autobus)
                  ->where('tractor',    '=', $with_tractor)
                  ->where('maquinaria', '=', $with_maquinaria)
                  ->where('furgoneta',  '=', $with_furgoneta)
                  ->where('taxi',       '=', $with_taxi);
            });

            $count = $query->count();
            $clients_counter = $count;
            $cost = $clients_counter * 0.08;
            $response = [
                'counter'   => $clients_counter,
                'cost'      => $cost,
                'inputed'   => Input::all()
            ];
            return $response;

$with_moto、$with_coche$with_taxi对应于我表格上的复选框。如果我一个接一个地检查(我的意思是只检查一个),我会得到正确的结果。

例如,如果我检查$with_moto,我得到2个结果,如果我查看$with_coche,我得到1个结果。当我检查这两个结果时,我需要达到的是得到3个结果。

所有这些字段都是tinyint(1),值为1或0。

我试了很多办法想办法解决这个问题,但我错过了一些东西。

这是一个SQL查询,我对SQL Server手动使用,我得到了正确的结果:

从客户端选择*其中(moil_1!="或moil_2!="或otro_movil!=")AND(recibe_sms='1')AND((moto='1'(coche='1')OR(camion='1'OR(maquinaria='1')OR(furgoneta='1';

也许有人能帮我。

非常感谢!

我通过如下更改代码解决了问题:

        $clients_counter = 0;           $cost = 0;
        $query = DB::table('clientes')->where('recibe_sms', '=', '1')           ->where(function($q)            {
            $q->orWhere('movil_1',      '<>', '')
              ->orWhere('movil_2',      '<>', '')
              ->orWhere('otro_movil',   '<>', '');
        });
        $i = [];
        $i['with_moto']         = (Input::has('moto')) ? true : false;          $i['with_coche']        = (Input::has('coche')) ? true : false;             $i['with_camion']       = (Input::has('camion')) ? true : false;            $i['with_autobus']      = (Input::has('autobus')) ? true : false;           $i['with_tractor']      = (Input::has('tractor')) ? true : false;           $i['with_maquinaria']   = (Input::has('maquinaria')) ? true : false;            $i['with_furgoneta']    = (Input::has('furgoneta')) ? true : false;             $i['with_taxi']         = (Input::has('taxi')) ? true : false;
                    $query = $query->where(function($q) use ($i)            {
            if ($i['with_moto']) $q->orWhere('moto',                '=', $i['with_moto']);
            if ($i['with_coche']) $q->orWhere('coche',              '=', $i['with_coche']);
            if ($i['with_camion']) $q->orWhere('camion',            '=', $i['with_camion']);
            if ($i['with_autobus']) $q->orWhere('autobus',          '=', $i['with_autobus']);
            if ($i['with_tractor']) $q->orWhere('tractor',          '=', $i['with_tractor']);
            if ($i['with_maquinaria']) $q->orWhere('maquinaria',    '=', $i['with_maquinaria']);
            if ($i['with_furgoneta']) $q->orWhere('furgoneta',      '=', $i['with_furgoneta']);
            if ($i['with_taxi']) $q->orWhere('taxi',                '=', $i['with_taxi']);
        });
                    $count = $query->count();
        $clients_counter = $count;          $cost = $clients_counter * 0.08;
        $response = [
            'counter'   => $clients_counter,
            'cost'      => $cost,
            'inputed'   => Input::all()
        ];
        return $response;