简体   繁体   English

Laravel:检查哪个表单提交了请求

[英]Laravel: Check on which form submitted the request

On my website, users can display posts created by other users. 在我的网站上,用户可以显示其他用户创建的帖子。 However, I have 2 select boxes, one for ordering by name or date and the other is ordering by category. 但是,我有2个选择框,一个用于按名称或日期排序,另一个用于按类别排序。 The way I have done it is incorrect and I am aware of that. 我这样做的方式是不正确的,我知道这一点。 The simple reason for this is that I don't know how to make an if check on which form submitted the request. 这样做的简单原因是,我不知道如何检查提交请求的表单。

Here is my controller method for displaying the events: 这是我用于显示事件的控制器方法:

public function getEvents(Request $request){
    if($request['sort'] == "created_at"){
        $posts = Post::orderBy('created_at', 'desc')->get();
    }
    elseif($request['sort'] == "title"){
        $posts = Post::orderBy('title', 'desc')->get();
    }
    elseif($request['category'] == "sport"){
        $post = Post::where('type', '=', 'sport' )->get();
    }
    elseif($request['category'] == "culture"){
        $post = Post::where('type', '=', 'culture' )->get();
    }
    elseif($request['category'] == "other"){
        $post = Post::where('type', '=', 'other' )->get();
    }
    else{
        $posts = Post::orderBy('created_at', 'desc')->get();
    }

    return view ('eventspage',  ['posts' => $posts]);
}

This is currently incorrect, I want it to follow this structure: 目前这是不正确的,我希望它遵循以下结构:

if(request submitted by 'sort')
   then do this...
elseif(request submitted by 'category')
   then do this...

Here is my view containing the 2 select boxes: 这是我的视图,其中包含2个选择框:

<div class="row">
    <div class="col-md-6">
        <form action="{{ route('events') }}">
            <select name="category" onchange="this.form.submit()" class="form-control">
                <option value="sport">sport</option>
                <option value="culture">Culture</option>
                <option value="other">Other</option>
            </select>
        </form>
    </div>
    <div class="col-md-6">
        <form action="{{ route('events') }}">    
            <select name="sort" onchange="this.form.submit()" class="form-control">
                <option value="created_at">Date</option>
                <option value="title">Title</option>
            </select>
        </form>
    </div>
</div>

You can have a hidden input in your form with name as formName and the respective values. 您可以在表单中具有名称为formName以及各自值的隐藏输入。

Then it's easy to check which form submitted. 然后,很容易检查提交的表单。

//Category form
<input type="hidden" name="formName" value="category">

//Sort form
<input type="hidden" name="formName" value="sort">

Then, in the controller: 然后,在控制器中:

if($request['formName'] == 'category') //request submitted by 'category'
   //then do this...
elseif($request['formName'] == 'sort')  //request submitted by 'sort'
   //then do this...

Just be careful not to put too much different code in both conditionals. 请注意不要在两个条件中放入太多不同的代码。 If you find yourself having two entirely different functions, create an action for each form. 如果发现自己有两个完全不同的功能,请为每个表单创建一个动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM