2014-04-28 174 views
0

我怎麼能這樣做的查詢中laravel:子查詢中laravel查詢生成器

SELECT * FROM(
SELECT * FROM `tb_horario` where cod_funcionario="'.$cod.'" and deleted_at is null) 
AS temp 
where temp.motivo!='' or temp.validado=0 

但有時我沒有使用cod_funcionario,因爲在我的網頁過濾

所以我一直是這樣的:

if ($funcionario) 
     {    
      $horariosQuery->where('cod_funcionario', $funcionario); 
     } 

我不知道如何做這個查詢與laravel中的這個子查詢,就像我在sql中做的那樣。 thxx!

+0

您是否找到過該產品? http://stackoverflow.com/questions/16815551/how-to-do-this-in-laravel-subquery-where-in –

+0

是的,我試圖做類似的事情,但我不知道我把它放在哪裏過濾器,在哪裏在子查詢之前或在子查詢裏面 –

+0

另外,我不明白你爲什麼需要子查詢。爲什麼你不能'和temp.motivo!=''或temp.validado = 0'? –

回答

1
$horariosQuery = $this->horario->with(array('funcionario', 'item_contabil')) 
           ->whereNull('deleted_at') 
           ->orderby('cod', 'ASC'); 

     if ($funcionario) 
     {    
      $horariosQuery->where('cod_funcionario', $funcionario) 
          ->where(function ($query) { 
           $query->where('validado', 0) 
             ->orWhere('motivo', '!=', ''); 
          }) 
          ->orderBy('data'); 
     } 
     else 
     { 
      $horariosQuery->where('validado', 0) 
          ->orWhere('motivo', '<>', '') 
          ->orderBy('cod_funcionario'); 
     }