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 查询,我得到了正确的结果:

SELECT * FROM clientes WHERE (movil_1 != "" OR movil_2 != "" OR otro_movil != "") AND (recibe_sms = '1') AND ((moto = '1') OR (coche = '1') OR (camion = '1') OR (autobus = '1') OR (tractor = '1') OR (maquinaria = '1') OR (furgoneta = '1') OR (taxi = '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;