好的,所以我想出了如何做到这一点。它可能不是最好或最优雅的解决方案,但它可以工作,我可以稍后对其进行重构。
在我看来,首先是形式。
{{ Form::open(array('action' => 'EventsController@postSearch', 'method' => 'POST', 'class' => 'view-only pull-left form-inline')) }}
{{ Form::label('courses', 'Course', array('class' => 'label')) }}
{{ Form::select('courses[]', $courses, '', array('class' => 'input-medium field', 'multiple' => 'multiple')) }}
{{ Form::label('suppliers', 'Supplier', array('class' => 'label')) }}
{{ Form::select('suppliers[]', $suppliers, '', array('class' => 'input-medium field', 'multiple' => 'multiple')) }}
{{ Form::label('locations', 'Location', array('class' => 'label')) }}
{{ Form::select('locations[]', $locations, '', array('class' => 'input-medium field', 'multiple' => 'multiple')) }}
{{ Form::label('venues', 'Venue', array('class' => 'label')) }}
{{ Form::select('venues[]', $venues, '', array('class' => 'input-medium field', 'multiple' => 'multiple')) }}
{{ Form::label('event_statuses', 'Status', array('class' => 'label')) }}
{{ Form::select('event_statuses[]', $event_statuses, '', array('class' => 'input-medium field', 'multiple' => 'multiple')) }}
{{ Form::submit('Search', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
还有我在我的 EventsController 中改进的 postSearch 方法:
public static function postSearch()
{
$courses_value = Input::get('courses');
$suppliers_value = Input::get('suppliers');
$locations_value = Input::get('locations');
$venues_value = Input::get('venues');
$status_value = Input::get('event_statuses');
// $tutor_value = Input::get('tutors');
$search_data = array(
'courses_value' => $courses_value,
'suppliers_value' => $suppliers_value,
'locations_value' => $locations_value,
'venues_value' => $venues_value,
'status_value' => $status_value,
// 'tutor_value' => $tutor_value
);
return Redirect::to('events')->with($search_data);
}
然后我的索引方法包含以下内容:
$courses_value = Session::get('courses_value');
$suppliers_value = Session::get('suppliers_value');
$locations_value = Session::get('locations_value');
$venues_value = Session::get('venues_value');
$status_value = Session::get('status_value');
// $tutor_value = Session::get('tutor_value');
if (is_null($courses_value)) {
$courses = Course::all();
$course_titles = array();
foreach ($courses as $course) {
$course_titles[] = $course->title;
}
$courses_value = $course_titles;
}
if (is_null($suppliers_value)) {
$suppliers = Supplier::all();
$supplier_names = array();
foreach ($suppliers as $supplier) {
$supplier_names[] = $supplier->name;
}
$suppliers_value = $supplier_names;
}
if (is_null($locations_value)) {
$locations = Location::all();
$location_cities = array();
foreach ($locations as $location) {
$location_cities[] = $location->city;
}
$locations_value = $location_cities;
}
if (is_null($venues_value)) {
$venues = Venue::all();
$venue_names = array();
foreach ($venues as $venue) {
$venue_names[] = $venue->name;
}
$venues_value = $venue_names;
}
if (is_null($status_value)) {
$event_statuses = EventStatus::all();
$statuses = array();
foreach ($event_statuses as $event_status) {
$statuses[] = $event_status->status;
}
$status_value = $statuses;
}
这可能与重构有关。无论如何,现在当我使用以下方法选择我的结果时:
$events = Event::where('active', '=', 1)
->leftJoin('courses', 'courses.id', '=', 'events.course_id')
->leftJoin('suppliers', 'suppliers.id', '=', 'events.supplier_id')
->leftJoin('locations', 'locations.id', '=', 'events.location_id')
->leftJoin('venues', 'venues.id', '=', 'events.venue_id')
->leftJoin('event_types', 'event_types.id', '=', 'events.event_type_id')
->leftJoin('event_statuses', 'event_statuses.id', '=', 'events.event_status_id')
->leftJoin('tutors', 'tutors.id', '=', 'events.tutor_id')
->orderBy($order[0], $order[1])
->select(array('events.*', DB::raw('events.id as eid'), DB::raw('suppliers.name as sname'), DB::raw('locations.city as lcity'), DB::raw('venues.name as vname'), DB::raw('venues.city as vcity'), DB::raw('tutors.first_name as tfirst_name')))
->whereIn('courses.title', $courses_value)
->whereIn('suppliers.name', $suppliers_value)
->whereIn('locations.city', $locations_value)
->whereIn('venues.name', $venues_value)
->whereIn('event_statuses.status', $status_value)
// ->whereIn('tutors.first_name', $tutor_value)
->paginate($perPage);
我现在可以在多选框中选择单个或多个值,结果按预期显示,这很棒。
可能有更好的方法来做到这一点,所以如果你们知道一个,请告诉我。
最好将提交数据存储在未从会话中检索到的变量中,这样我就可以对结果使用分页,并且用户在刷新或移离和移回时不必重新搜索这页纸。