我有一个laravel 4项目,我需要使用高级搜索创建一个模块,这是我无法绕过头脑的事情。
这是我的表格:
<div class="form-group">
{{ Form::open(['route' => 'requests.search']) }}
{{ Form::label('status', 'Status:') }}
{{ Form::select('status', $statuses, null, ['class' => 'form-control']) }}
</div>
<!-- Category Form Input -->
<div class="form-group">
{{ Form::label('category', 'Category:') }}
{{ Form::select('category', $categories, null, ['class' => 'form-control']) }}
</div>
<!-- Teamleader Form Input -->
<div class="form-group">
{{ Form::label('teamleader', 'Team Leader:') }}
{{ Form::select('teamleader', $projectmembers, null, ['class' => 'form-control']) }}
</div>
<!-- Requestid Form Input -->
<div class="form-group">
{{ Form::label('requestid', 'Request ID:') }}<br/>
<span>Between:</span>
{{ Form::text('requestidstart', null, ['class' => 'form-control', 'style' => 'width:100%;']) }}
<span>And:</span>
{{ Form::text('requestidend', null, ['class' => 'form-control', 'style' => 'width:100%;'])}}
</div>
<!-- Requestdate Form Input -->
<div class="form-group">
{{ Form::label('requestdate', 'Request Date:') }}<br/>
<span>From:</span>
{{ Form::text('requestdatestart', null, ['class' => 'form-control etadatepicker', 'style' => 'width:100%;']) }}
<span>To:</span>
{{ Form::text('requestdateend', null, ['class' => 'form-control etadatepicker', 'style' => 'width:100%;'])}}
</div>
<!-- Requestduedate Form Input -->
<div class="form-group">
{{ Form::label('requestduedate', 'Due Date:') }}<br/>
<span>From:</span>
{{ Form::text('requestduedatestart', null, ['class' => 'form-control etadatepicker', 'style' => 'width:100%;']) }}
<span>To:</span>
{{ Form::text('requestduedateend', null, ['class' => 'form-control etadatepicker', 'style' => 'width:100%;'])}}
</div>
{{ Form::hidden('search', 'advanced') }}
{{ Form::submit('Search') }}
{{ Form::close() }}<br/>
在我的控制器中,我有以下开关确定用户是在进行简单的文本搜索还是高级搜索:
switch($search)
{
case 'text':
$requests = DataRequest::search($query, ["id", "userid", "subject", "details", "eta", "leader", "member1", "member2", "member3", "member4", "member5", "member6", "status", "time_spent", "date_completed", "category", "comments", "response_method", "created_at", "updated_at"])
->paginate(10);
return View::make('requests.index', ['requests' => $requests, 'statuses' => $statuses, 'requestresponsetype' => $requestresponsetype, 'projectmembers' => $projectmembers, 'categories' => $categories, 'query' => $query]);
break;
case 'advanced':
break;
default:
$requests = DataRequest::orderBy('id', 'DESC')->paginate(10);
return View::make('requests.index', ['requests' => $requests, 'statuses' => $statuses, 'requestresponsetype' => $requestresponsetype, 'projectmembers' => $projectmembers, 'categories' => $categories, 'query' => $query]);
break;
}
显然,正如我针对文本案例所做的那样,我知道如何使用输入来查询数据库以进行简单的文本搜索。
我将如何处理每个字段,利用他们的所有查询,并返回满足“高级”字段中所有查询的结果。情况?
另外,我知道我可以这样做:
$requests = DataRequest::where('category', '=', $advancedCategories)
->orWhere('status', '=', $advancedStatus)
->orWhere(...)
->orWhere(...)
->orWhere(...)
->orWhere(...)
->orWhere(...)
->paginate(10);
但是如果其中一个字段为空,则会出错:1054未知列&#39;&#39;在&#39; where子句&#39;
答案 0 :(得分:2)
从输入传递空值的orWhere
条件不会抛出任何MySQL错误,条件只是OR WHERE column = ""
。但是,这并不是您可能想要的条件,因为它将在结果集中包含该列为空的所有条目。
以下是我如何处理它以保持清洁并只添加所需的条件:
// Define an array with the names of the input parameters for advanced search
$searchParams = ['category', 'teamleader', 'requestidstart', 'requestidend', 'requestdatestart', 'requestdateend', 'requestduedatestart', 'requestduedateend'];
// Create a blank query for your model on which you can add conditions
$query = DataRequest::query();
// Add only the params that have been passed values from the form as conditions
foreach ($searchParams as $param) {
if (Input::has($param)) {
$query->orWhere($param, Input::get($param));
}
}
// Get your paginated results
$requests = $query->paginate(10);