简体   繁体   English

Laravel:如何将表单中的值传递给控制器​​?

[英]Laravel: how do I pass a value from a form to a controller?

I have a form: 我有一个表格:

{ Form::open(array('action' => 'RatesController@postUserRate', $client->id)) }}
    {{ Form::text('rate', '', array('placeholder' => 'Enter new custom client rate...')) }}
    {{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}

How do I pass my $client->id value through the form to my controller method? 如何通过表单将$ client-> id值传递给我的控制器方法?

I currently have a controller method that looks like this: 我目前有一个控制器方法,如下所示:

public function postUserRate($id)
{
    $currentUser = User::find(Sentry::getUser()->id);
    $userRate = DB::table('users_rates')->where('user_id', $currentUser->id)->where('client_id', $id)->pluck('rate');

    if(is_null($userRate))
    {
    ...
    }else{
    ....
    }
}

And the error log says "Missing argument 1 for RatesController::postUserRate()" 并且错误日志显示“对于RatesController :: postUserRate()缺少参数1”

Any ideas on how to pass this $client->id into my controller so I can use it as I want to above? 关于如何将这个$ client-> id传递给我的控制器的任何想法,所以我可以按照我想要的那样使用它?

Add {{ Form::hidden('id', $client->id) }} to the form. {{ Form::hidden('id', $client->id) }}到表单中。 Then, when it's posted, you can fetch its value per usual with Input::get('id') . 然后,当它被发布时,您可以使用Input::get('id')每平常获取其值。

Also, remove the postUserRate method's argument. 另外,删除postUserRate方法的参数。

You simply use : 你只需使用:

Form::open(array('action' => array('Controller@method', $user->id)))

  • the variable $user->id is passed as argument to the method method , also this last one should recieve an argument as well, like so : method($userId) 变量$user->id作为参数传递给方法method ,最后一个也应该接收一个参数,如下所示: method($userId)

Source : Laravel documentation 来源:Laravel 文档

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

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