2015-02-23 74 views
-1

我有下面的代碼的問題:Laravel 5複雜和WHERE查詢不能如預期

  $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 = 「」!OR movil_2 = 「」 或otro_movil = (「=」1「)AND((moto ='1'))OR(自動編程='1')或(拖拉機='1')OR (coche ='1' 1') OR(maquinaria ='1')OR(furgoneta ='1')OR(taxi ='1'));

也許有人可以幫助我。

非常感謝!

+0

請提出這樣的問題。 – kenorb 2015-02-23 21:43:25

+0

您正在尋找'orWhere'而不是'where' – 2015-02-23 21:49:00

+0

我試圖解釋我需要在代碼下面實現的內容。如果我檢查一個複選框(moto),我會得到2個結果,如果我檢查其他複選框(例如'coche'),我會得到1個結果。我應該如何使查詢得到3個結果 – vitaminasweb 2015-02-23 21:51:55

回答

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;