向Laravel 4中的操作提交多个选择值 [英] Submit Multiple Select Values to an Action in Laravel 4

查看:51
本文介绍了向Laravel 4中的操作提交多个选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找出如何使用重定向或类似于Laravel 4中资源控制器中的index()方法/操作的方式提交多个选择值的方法.

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.

在我看来,这是表格,带有多个多选框. http://paste.laravel.com/s1n

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

此刻,它正在发布到我的EventsController中名为postSearch()的操作/方法中. http://paste.laravel.com/s1p 然后,该方法重定向到我的index()方法,并且应该使用$ search_data变量,然后使用 http://paste从Eloquent模型中选择结果. laravel.com/s1q

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.

推荐答案

好的,所以我想出了方法.它可能不是最佳或最优雅的解决方案,但它可以工作,以后我可以对其进行重构.

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.

我认为首先是表格.

{{ 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方法:

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.

这篇关于向Laravel 4中的操作提交多个选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆