首先以为laravel 对DB的多条件查询支持的不是很好,后来仔细看了下API文档,是可以在where()里面用匿名函数去处理条件的。
代码如下:
public function index() { $table = "RunLogInfo_" . date("Ym", time()); // $start_date = $this->request->input('begin_date'); // $end_date = $this->request->input("end_date"); $log_level = $this->request->input("log_level"); $hostname = $this->request->input("hostname"); $errLogList = DB::table($table) ->where( function ($query) use ($hostname) { if (isset($hostname)) { $query->where("Hostname", "like", "%" . $hostname . "%"); } } ) ->where(function ($query) use ($log_level) { if (-1 != $log_level) { $query->where('LogLevel', '=', $log_level); } }) ->paginate(1); return view("errorlog/index")->with("errlogList", $errLogList); }