2017-02-13 84 views
0

這是我目前的QB。laravel多個select語句相互覆蓋

$institutes = count($request->input("selected_institutes")); 
$courses = count($request->input("selected_courses")); 
$institutes_data = $request->input("selected_institutes"); 
$courses_data = $request->input("selected_courses"); 
$search=DB::table('users as u'); 
if($institutes > 0 && $courses = 0){ 
    $search=$search->select('u.id','u.first_name','u.profile_picture','inst.full_name') 
    ->leftJoin('user_institutes as ins','ins.user_id','=','u.id') 
    ->leftJoin('institutes as inst','inst.id','=','ins.institute_id'); 
} 
else if($courses > 0 && $institutes = 0){ 
    $search=$search->select('u.id','u.first_name','u.profile_picture','crse.full_name') 
    ->leftJoin('user_courses as crs','crs.user_id','=','u.id') 
    ->leftJoin('courses as crse','crse.id','=','crs.course_id'); 
} 
else if($institutes > 0 && $courses > 0){ 
    $search=$search->select('u.id','u.first_name','u.profile_picture','inst.full_name','crse.full_name') 
    ->leftJoin('user_institutes as ins','ins.user_id','=','u.id') 
    ->leftJoin('institutes as inst','inst.id','=','ins.institute_id') 
    ->leftJoin('user_courses as crs','crs.user_id','=','u.id') 
    ->leftJoin('courses as crse','crse.id','=','crs.course_id'); 
} 
$search=$search->WhereNull('u.deleted_at'); 
for($i=0;$i<$institutes;$i++){ 
    $search=$search->orWhere('inst.full_name','=',"`$institutes_data[$i]`"); 
} 
for($i=0;$i<$courses;$i++){ 
    $search=$search->orWhere('crse.full_name','=',"`$courses_data[$i]`"); 
} 
$search=$search->get(); 

結果是基於輸入,給定的情景是 $院所= 2和$課程= 2, 我打印出來 ,我希望它是落在別人第二,如果是選擇查詢示爲下面

'select * from `users` as `u` where `u`.`deleted_at` is null or `inst`.`full_name` = ?', array('`institutes 1`') or `inst`.`full_name` = ?', array('`institutes 2`') 
根據這種情況的查詢我想如下

select('u.id','u.first_name','u.profile_picture','inst.full_name','crse.full_name') 
leftJoin 'user_institutes as ins' on 'ins.user_id''=''u.id' 
leftJoin 'institutes as inst' on 'inst.id''=''ins.institute_id' 
leftJoin 'user_courses as crs' on 'crs.user_id''=''u.id' 
leftJoin 'courses as crse' on 'crse.id''=''crs.course_id' 
where 'u.deleted_at' is null 
and ('inst.full_name' = 'institutes 1' 
or 'inst.full_name' = 'institutes 2') 
and ('crse.full_name' = 'courses 1' 
or 'crse.full_name' = 'courses 2'} 

任何人都可以執行克

請使用這些if else條件構造我的QB併爲where子句變量輸入運行for循環。 (我的語法道歉)

+0

什麼被覆蓋? –

+0

也許你應該只添加另一個如果$ institutes = 2和$ courses = 2的具體數量。 – Grynets

+0

@ jedrzej.kurylo,select語句被覆蓋,但我試圖修改QB,這是最新的輸出只是不如我在尋找 – kearn

回答

0

我的理解是你想做一個條件查詢,它具有查詢連接根據用戶輸入(篩選條件), 我已經做了以下例子如何在Laravel按照我的示例,嘗試重新編寫調整查詢。請注意,您可以在關閉時添加whereorWhere條件的數量。

$search = DB::table('users as u') 
     ->select(['u.id','u.first_name','u.profile_picture','inst.full_name']) 
     ->whereNull('u.deleted_at') 
     ->when(($request->has("selected_institutes")) && (!$request->has("selected_courses")) , function ($query) use ($request) { 
      return $query->leftJoin('user_institutes as ins','ins.user_id','=','u.id') 
         ->leftJoin('institutes as inst','inst.id','=','ins.institute_id'); 

     }) 

     ->when(($request->has("selected_courses")) && (!$request->has("selected_institutes")), function ($query) use ($request) { 
      return $query->leftJoin('user_courses as crs','crs.user_id','=','u.id') 
       ->leftJoin('courses as crse','crse.id','=','crs.course_id'); 

     }) 

     ->when($request->has("selected_institutes") && $request->has("selected_courses"), function ($query) use ($request) { 
      return $query ->leftJoin('user_institutes as ins','ins.user_id','=','u.id') 
       ->leftJoin('institutes as inst','inst.id','=','ins.institute_id') 
       ->leftJoin('user_courses as crs','crs.user_id','=','u.id') 
       ->leftJoin('courses as crse','crse.id','=','crs.course_id'); 

     }) 
     ->get();