2

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?

5
  • Is there a reason you can't just add a hidden field named q and set it's value to 54? That way it can be treated just like any other GET or POST variable. Commented Feb 12, 2014 at 13:17
  • It's just less tidy. I dont want to add whole field just to alter behaviour slightly. Commented Feb 12, 2014 at 13:21
  • Are you using POST or GET? Commented Feb 12, 2014 at 13:28
  • It's a POST, but does it really matter? Because I just want the "action" to include Q=whatever Commented Feb 12, 2014 at 13:39
  • Whether the form is GET or POST does matter - a GET form can't specify the GET params in the action using the question mark syntax, as it gets dropped as the form generates the URL from its inputs. As for your specific question though, I'm not sure. I say check out the UrlGenerator and FormBuilder code and see if they handle appending a query string to a URL. Commented Feb 12, 2014 at 14:12

2 Answers 2

4

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');
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.