原理很簡單,第一步:判斷接收的值,第二步:寫查詢語句。具體實現以下:laravel
//首先,建立句柄: segmentfault
$handle = DB::table('classrooms');spa
//判斷接收的值是否爲空 $keywords1
&& $handle->where('field_name','like','%' . $keywords1 . '%');
$keywords1
$keywords1
$handle->where('field_name','like','%' . $keywords1 . '%');也能夠寫成:if($keywords1){
$keywords1){
// 判斷接收的值是否爲空 $keywords2 && $handle->where('field_name','like','%' . $keywords2 . '%'); ... // 獲取數據 $handle->get();
代碼能夠精簡以下:
$handle = DB::table('classrooms');
$keys = $request->all();
foreach($keys as $key => $val){
$keys[$key] && $handle->where($key,'like','%' . $val . '%');
}
// 獲取數據
$datas = $handle->get();摘自:https://segmentfault.com/q/1010000007556256?_ea=1386542