我遇到以下代码的问题:
$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 Server手动使用的SQL查询,我得到了正确的结果:
SELECT * FROM clientes WHERE(movil_1!=&#34;&#34; OR movil_2 !=&#34;&#34;或者otro_movil!=&#34;&#34;)AND(recibe_sms =&#39; 1&#39;)AND((moto =&#39; 1&#39;)或 (coche =&#39; 1&#39;)或(camion =&#39; 1&#39;)或(autobus =&#39; 1&#39;)或者(拖拉机=&#39; 1&#39;) ) 或(maquinaria =&#39; 1&#39;)或(furgoneta =&#39; 1&#39;)或(出租车=&#39; 1&#39;);
也许有人可以帮助我。
非常感谢!
答案 0 :(得分: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;