Submit Multiple Select Values to an Action in Lara

2019-09-15 00:30发布

问题:

I've been trying to figure out how to submit multiple multi-select values using a redirect or something similar to an index() method/action in my resource controller in Laravel 4.

I've made several attempts to achieve this but have yet to work out how to do it.

I initially had it submitting to a query string, which worked to a certain extent but as soon as you selected more than 1 value in a single multi-select box, it wouldn't work, as it simply chose to use the last value.

This is the form in my view, with several multi-select boxes. http://paste.laravel.com/s1n

At the moment it's posting to an action/method in my EventsController called postSearch(). http://paste.laravel.com/s1p This method then redirects to my index() method and is supposed to take with it a $search_data variable to then be used for selecting results from an Eloquent model using http://paste.laravel.com/s1q

Now these methos are a bit of a mess at the moment, as I have been trying several things to get this working,

If anyone can offer any guidance on this I would really appreciate it. Thanks.

回答1:

OK, so I've figured out how to do this. It may not be the best or most elegant solution but it works and I can refactor it later on.

First in my view, is the form.

{{ 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() }}

And my refined postSearch method in my EventsController:

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);
}

Then my index method contains the following:

    $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;
    }

This is the bit that could probably do with refactoring. Anyway, now when I select my results using:

$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);

I am now able to select single or multiple values in the multi-select boxes and the results show as expected, which is great.

There's probably a much better way of doing this, so if you guys know of one please let me know.

What would be nice is to store the submission data in a variable that is not retrieved from the session, so I can use pagination on the results and the user doesn't have to re-search if they refresh or move away from and back to the page.