2017-04-14 34 views
0

我嘗試運行此查詢,一切都很好,直到:兩個查詢子句,其中

- >其中( 'statut_licence_id',[ '1', '4'])

這第二個在哪裏似乎不工作,它只顯示我statut_licence_id = 1而不是4,現在有人如何解決這個問題?

 $licencies = Licencies::whereIn('structure_id', $structures->pluck('id'))->whereIn('type_licence_id', ['1', '2', '3', '4', '5']) 
      ->whereIn('statut_licence_id', ['1' , '4']) 
      ->where('valid_licence_id', '1') 
      ->where('lb_assurance_etat' ,'=' , 'Assurée') 
      ->orderBy('created_at', 'DESC') 
      ->paginate(10); 

問題解決:

$licencies = Licencies::whereIn('structure_id', $structures->pluck('id'))->whereIn('type_licence_id', ['1', '2', '3', '4', '5']) ->where(function ($query) { 
      $query->whereIn('statut_licence_id', ['1' , '4']) 
      ->where('valid_licence_id', '1'); }) 
      ->orderBy('created_at', 'DESC') 
      ->paginate(10); 
+0

嘗試打印原始查詢。使用'DB :: enableQueryLog()'和'DB :: getQueryLog()' –

+0

我應該在哪裏? –

+0

在查詢之前放入'DB :: enableQueryLog()'。並在你的查詢之後加入'var_dump(DB :: getQueryLog())''。 –

回答

2

嘗試使用閉包。

$licencies = Licencies::whereIn('structure_id', $structures->pluck('id'))->whereIn(function ($query){ 
      $query->whereIn('type_licence_id', ['1', '2', '3', '4', '5']) 
      ->whereIn('statut_licence_id', ['1' , '4']); 
     }) 
     ->where('valid_licence_id', '1') 
     ->where('lb_assurance_etat' ,'=' , 'Assurée') 
     ->orderBy('created_at', 'DESC') 
     ->paginate(10); 
1

您可以按照下列步驟檢查您的查詢語句:

首先,前聲明:

DB::enableQueryLog(); 

其次,使用dd在聲明後打印您的sql:

dd(DB::getQueryLog()); 

最後,將sql複製並粘貼到你的數據庫客戶端中,你會得到你想要的。而且你不需要再次向類似的人詢問。