在 Laravel 5 中提交表单时重定向回同一页面(带有变量)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36438071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:32:57  来源:igfitidea点击:

Redirect back to same page (with variables) on form submit in Laravel 5

laravellaravel-5

提问by user3489502

On my page events.index, I first display a list of events for the logged on user.

在我的页面 events.index 上,我首先显示登录用户的事件列表。

On my index page I have a form with option/select to let the user select and display the events of another user. When he submits that form, I would like my index function (controller) to use the $user_id value (from the form) and display the events.index page again, but for events of that selected user.

在我的索引页面上,我有一个带有选项/选择的表单,让用户选择并显示另一个用户的事件。当他提交该表单时,我希望我的索引函数(控制器)使用 $user_id 值(来自表单)并再次显示 events.index 页面,但对于所选用户的事件。

I'm not sure what would be the best approach:

我不确定最好的方法是什么:

  • Set a session variable to keep the user_id value? Not sure how to do that with a form.
  • Submit the form with a get method (and get an ugly ?user_id=1 URL)
  • Change my index route to accept the post method (although I already have that post/events route taken (by Route::post('events', 'EventsController@store'))
  • 设置会话变量以保留 user_id 值?不知道如何用表格来做到这一点。
  • 使用 get 方法提交表单(并获得一个丑陋的 ?user_id=1 URL)
  • 更改我的索引路由以接受 post 方法(尽管我已经采用了 post/events 路由(通过 Route::post('events', 'EventsController@store'))

Not sure what would be a clean way to do this:

不知道这样做的干净方法是什么:

My route for events/index:

我的事件/索引路线:

Route::get('events', [
'as' => 'event.index',
'uses' => 'EventsController@index'
]);

Events Controller

事件控制器

public function index()
{
    // How to get the $user_id value from form?

    if (empty($user_id)) 
    {
        $user_id = \Auth::user()->id;
    }

    $events = Event::where('events.user_id','=','$user_id');        
    $users  = User::all();

    return view('events.index')->with(['events' => $events])->with(['users' => $users]);
}

View for index

查看索引

{!! Form::open(['route' => 'events.index', 'method' => 'get']) !!}

    <select id="user_id" name="user_id">
    @foreach($users as $user)
        <option value="{{$user->id}}">{{$user->name}}</option>
    @endforeach
    </select>

{!! Form::submit('Show events for this user') !!}

{!! Form::close() !!}   


@foreach($events as $event)
     ...
@endforeach

回答by Angad Dubey

You can get the user_idfrom a Requestobject, you just need to inject it in the index method:

你可以user_id从一个Request对象中获取,你只需要在 index 方法中注入它:

public function index(Request $request)
{
    $user_id = $request->get('user_id') ?: Auth::id();

    $events = Event::where('events.user_id','=','$user_id')->get();

    $users  = User::all();

    return view('events.index')->with(['events' => $events])->with(['users' => $users]);
}