Laravel:如何在要在 Route::post 中使用的表单 POST 中使用参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40612780/
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 14:45:00  来源:igfitidea点击:

Laravel : How to use a parameters in a Form POST to be use in a Route::post?

phphtmllaravel

提问by Vahn Marty

This my current POST route. Route::post('/eAPI', 'ApiController@eAPI');

这是我目前的 POST 路线。 Route::post('/eAPI', 'ApiController@eAPI');

I wanted to make it like

我想让它像

Route::post('/q={$number}', 'ApiController@eAPI');

Route::post('/q={$number}', 'ApiController@eAPI');

But in my form.

但以我的形式。

<form action="{{url('/eAPI')}}" method="post" id="search">
  <div class="form-group">
    <label for="number" class="col-md-4 control-label">Telephone Number to search :</label>

    <div class="col-md-6">
      <input class="form-control" id="number" name="number" placeholder="Phone (eg. 5551234567)" required>


    </div>
  </div>

  <div class="col-md-2">
    <input type="submit" name="name" value="Find" class="btn btn-success">
  </div>
</form>

Now, I want to put a variable in this part, something like this.

现在,我想在这部分放一个变量,像这样。

<form action="{{url('/?q=$number')}}" method="post" id="search">

<form action="{{url('/?q=$number')}}" method="post" id="search">

回答by Saumya Rastogi

In post request you should do it like this:

在发布请求中,您应该这样做:

Route::post('/eAPI/{q}', 'ApiController@eAPI')->name('my_route');

And in HTML Form:

并在 HTML 表单中:

<form action="{{ route('my_route', ['q' => '4']) }}" method="post" id="search">
</form>

And inside controller you can retrieve it as:

在控制器内部,您可以将其检索为:

Class ApiController {

    public function eAPI($q) {
        // Use $q here ...
    }

}

Hope this helps!

希望这可以帮助!

回答by Alexey Mezenin

I never did and will never do this with postrequests, but it works with getrequests:

我从来没有也永远不会对post请求这样做,但它适用于get请求:

$q = request()->q;

And you don't need to add this to the route: q={$number}, just add parameters to url: ?q=value1&s=value2&c=value3

并且您不需要将此添加到 route: q={$number},只需将参数添加到 url:?q=value1&s=value2&c=value3

回答by Ian Romie Ona

This works for me [method post and url has ?q=someValue] :

这对我有用 [method post and url has ?q=someValue] :

 public function eApi(Request $request){
     $q = $request['q'];
 }

This code will get all params in post and get method

此代码将在 post 和 get 方法中获取所有参数

$request->all()

Hope it helps!

希望能帮助到你!