2016-04-13 62 views
0

嗨可以問一些幫助它沒有輸出這個原始查詢,原始查詢沒有輸出

$userproj_id = UserProject::select('projid')->where('user_id', $id)->get()->toArray(); 
     $flattenid = array_flatten($userproj_id); 
     foreach ($flattenid as $projid) { 
      $projidarr [] = $projid; 
     } 


    $users = DB::select('select * from project where projid in (?) and user_id = 3 ', $projidarr); 

,但如果我將手動做這樣的,它工作正常..

$users = DB::select('select * from project where projid in (?) and user_id = 3 ', ['12345']); 

但當我手動執行此操作時不再輸出。

$users = DB::select('select * from project where projid in (?) and user_id = 3 ', ['12345','11111']); 

在此先感謝您。

+0

http://stackoverflow.com/a/35725251/4248472? – Zimmi

回答

0

這是因爲在你的綁定數組中,你必須有很多參數--SQL只需要一個參數,而你提供了兩個參數。 適當的參數列表應該是[implode(',',[12345,1111])],但在這種情況下,它將是一個字符串,結果也不會正確

這是怎麼回事?

DB::table('project') 
->whereIn('projid', [12345, 1111]) 
->where('user_id', '=', 3); 
0

原始查詢:

$users = DB::select('select * from project where projid in ('. implode(',', $projidarr).') and user_id = 3 '); 

和Laravel查詢:

DB::table('project') 
->whereIn('projid', $projidarr) 
->where('user_id', '=', 3);