简体   繁体   中英

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?

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()"

Any ideas on how to pass this $client->id into my controller so I can use it as I want to above?

Add {{ Form::hidden('id', $client->id) }} to the form. Then, when it's posted, you can fetch its value per usual with Input::get('id') .

Also, remove the postUserRate method's argument.

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)

Source : Laravel documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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