如果请求到来然后在查询上连接表,则不需要其他明智的连接

时间:2018-04-07 10:16:28

标签: php laravel

如果设施出现请求,则需要加入,否则不需要使用 library_members 居民表等设施加入查询。

public function add(Request $request)
    {    
   $facility = $request->get('facility');
    $data['student'] = Student::select('students.id','students.reg_no','students.reg_date', 'students.first_name', 'students.middle_name', 'students.last_name','students.faculty', 'students.semester','students.status')            
                ->join('library_members as l','l.reg_no','=','students.reg_no')
                ->join('residents as r','r.reg_no','=','students.reg_no')
                ->get();
}

1 个答案:

答案 0 :(得分:1)

public function add(Request $request)
{   
    if($request->has('facility')){
        $facility = $request->get('facility');
        $data['student'] = Student::select(
                                'students.id','students.reg_no',
                                'students.reg_date', 'students.first_name', 
                                'students.middle_name', 'students.last_name',
                                'students.faculty', 'students.semester','students.status'
                            )            
                            ->join('library_members as l','l.reg_no','=','students.reg_no')
                            ->join('residents as r','r.reg_no','=','students.reg_no')
                            ->get();    
    }else{
        $data['student'] = Student::select('fields')->get();
    }

}