将参数从表单传递给控制器​​ Laravel

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

Pass parameter from form to Controller Laravel

phplaravellaravel-5

提问by FilippoLcr

I'm trying to understand how to pass parameters via URL in Laravel.

我试图了解如何在 Laravel 中通过 URL 传递参数。

In my case I've a home page (/) (HomeController@get__home and view home) that contains n data get from a database table. The user can select one of them and go to the next page.

就我而言,我有一个主页 (/)(HomeController@get__home 和 view home),其中包含从数据库表中获取的 n 个数据。用户可以选择其中一个并转到下一页。

The second url page is /{param from first page}/login

第二个 url 页面是 /{param from first page}/login

The field {param from first page} comes from (of course) first page and depends on which record was selected.

字段 {param from first page} 来自(当然)第一页,取决于选择的记录。

I've read this, but I think I'm out of the way.

我读过这个,但我想我已经不在了。

I can not find a way to pass the parameter to the url.

我找不到将参数传递给 url 的方法。

In my Route.php:

在我的 Route.php 中:

Route::get("/", "HomeController@home");
Route::get("/{position}/login", "LoginController@login");

and in Controllers:

并在控制器中:

class HomeController extends Controller
{
    public function home(){

      $foos = Foo::all();

      return view('home')->with('foos',$foos);
    }
}

class LoginController extends Controller
{
  public function login(Foo $foo)
  {
    return view('login');
  }
}

and in home.blade.php

并在 home.blade.php

<form class="" action="{{action(LoginController@login)}}" method="post">
...

回答by Laerte

Since you're using a form with post method, you should define the route like this:

由于您使用的是带有 post 方法的表单,因此您应该像这样定义路由:

Route::post("/login", "LoginController@login");

And then call it in the form action:

然后在表单操作中调用它:

<form action="{{action('LoginController@login')}}" method="post">
    <select name="position">
        <option value="1">Position 1</option>
        <option value="2">Position 2</option>
    </select>
...

Then, in the controller, you can get the option in the request:

然后,在控制器中,您可以获得请求中的选项:

class LoginController extends Controller
{
    public function login(Request $request)
    {
        $position = $request->position;
        return view('login');
    }
}