如何在Laravel 4.2中进行多个输入过滤的搜索?

时间:2015-09-28 17:55:04

标签: php laravel laravel-4

我希望此表单能够搜索多个字段。如何汇总这些搜索字词并按其相应字段对其进行过滤?

例如,如果用户在“文本搜索”字段中键入“预订”,然后从状态下拉列表中选择“已分配”,则该页面应返回主题或详细信息字段中包含“预订”的所有请求,并且“已分配”在状态字段中。

我在Laravel 4.2应用程序中有以下表格:

    {{ Form::open(['route' => 'requests.search']) }}
    <!-- Text Search Form Input -->
    <div class="form-group">
        {{ Form::label('textsearch', 'Text Search:') }}
        {{ Form::text('textsearch', $query, ['class' => 'form-control']) }}
        {{ Form::hidden('search', 'text') }}
    </div>
    <!-- Status Form Input -->
    <div class="form-group">
    {{ 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::submit('Search') }}
    {{ Form::close() }}<br/>

控制器:

$requestresponsetype = ['' => ''] + RequestResponseType::lists('description', 'description');
$projectmembers = ['' => ''] + RequestProjectMember::lists('first_name', 'first_name');
$statuses = ['' => ''] + RequestStatus::lists('description', 'description');
$categories = ['' => ''] + RequestCategory::lists('description', 'description');
$query = Input::get('textsearch');
$requests = DataRequest::where("subject", "LIKE", "%$query%")->orWhere("details", "LIKE", "%$query%")->orWhere("id","LIKE","%$query%")->paginate(10);
return View::make('requests.index', ['requests' => $requests, 'statuses' => $statuses, 'requestresponsetype' => $requestresponsetype, 'projectmembers' => $projectmembers, 'categories' => $categories, 'query' => $query]);

1 个答案:

答案 0 :(得分:1)

我不明白你的想法,但我猜是这样的:

$requests = DataRequest::where(function($q) {
    $q->where("subject", "LIKE", "%$query%")
      ->orWhere("details", "LIKE", "%$query%")
      ->orWhere("id","LIKE","%$query%");
})
->where('status', '=', $status)
->paginate(10);

在我的项目中,我创建了一个搜索范围,它可以重复使用并且可读。

//BaseModel
public function scopeSearch($query, $search, $fields)
{
    $query->where(function($q) use ($search, $fields) {
        foreach ($fields as $field) {
            $q->orWhere($field, "LIKE", "%$search%");
        }
    }
}

$requests = DataRequest::search($query, ["subject", "details", "id"])
->where('status', '=', $status)
->paginate(10);