rockstardev
rockstardev

Reputation: 13527

Laravel: Adding a query parameter in the url of a form?

It's quite easy to open a form that will submit to a specific url:

echo Form::model($model, array('route' => array('user', $user->id)))

This will render something like this:

<form action="/user/1">

I want:

<form action="/user/1?q=54">

In other words, how can I change the form method to support the addition of a query parameter, in this case called "q"?

A simpler way of asking this would be: is there a way to generate a URL using Route's so as to include a query paramter?

Upvotes: 2

Views: 5929

Answers (2)

Ben K.
Ben K.

Reputation: 509

Had the same issue, solved by adding a hidden field in the form and calling it from the controller.

{{ Form::hidden('_test', 'tags') }}

In the controller be sure to set this:

public function update($id)
{
    $test= Input::get('_test', 'default');
    $input = array_except(Input::all(), '_method', '_test');
    // prevent from updating an inexistent field
...

Can't comment @Steve's answer cause of restrictions, will update in here:

It won't work because "Form::model" will process the value in "HTML::entities()", so

$id . "?test"

is going to be

1%3Ftest

Upvotes: 0

Steve Bauman
Steve Bauman

Reputation: 8668

Unfortunately the only answer I've found to this question is appending on the variable onto the URL like this:

echo Form::model($model, array('route' => array('user', $user->id.'?q=54')))

Though why don't you just use SEO friendly parameters like normal? So it would come out like

<form action="/user/1/54">

You can also use a route with parameters to make your URL's however you want them to look such as (I'm assuming the Q in your code means question:

Route::post('user/{id}/q/{q_id}', 'UserController@postQuestion');

Or

Route::post('user/{id}/question/{q_id}', 'UserController@postQuestion');

Upvotes: 4

Related Questions