Josh
Josh

Reputation: 6139

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?

Upvotes: 2

Views: 19038

Answers (2)

zianwar
zianwar

Reputation: 3740

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

Upvotes: 3

JeffreyWay
JeffreyWay

Reputation: 1143

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.

Upvotes: 17

Related Questions