这是我目前的QB。
$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();
结果是基于输入,给定的场景是 $ institutes = 2和$ courses = 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)
我的理解是你想要创建一个条件查询,它根据用户输入(过滤条件)进行查询连接,
我已经做了以下示例如何以Laravel的方式实现这一点,尝试根据我的示例重新编写调整查询..请注意,您可以在闭包时添加where
或orWhere
条件的数量。
$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();