8

I'm facing a big wall of larval routing and I can't seem to find a solution

I have this form in a view template

<form url="/request/{{$equipment->url}}" method="POST">
 <div class="row">
 <div class="col-sm-4">
 <div class="mt10">Start Date:</div>
 <input type="date" required name="starting_date" value="" placeholder="From" class="request-input request-date mb10">
 </div>
 <div class="col-sm-4">
 <div class="mt10">End Date:</div>
 <input type="date" required name="ending_date" value="" placeholder="To" class="request-input request-date mb10">
 </div>
 <div class="col-sm-4">
 <div class="mt10">Quantity</div>
 <input type="number" required name="quantity" value="" placeholder="Quantity" class="request-input mb10">
 </div>
 </div>
 <div class="row">
 <div class="col-sm-4">
 <div class="mt10">Voltage</div>
 <input type="number" required name="voltage" value="" placeholder="Voltage" class="request-input mb10">
 </div>
 <div class="col-sm-4">
 <div class="mt10">Param 1</div>
 <input type="text" required name="param_1" value="" placeholder="Parameter" class="request-input mb10">
 </div>
 <div class="col-sm-4">
 <div class="mt10">Param 2</div>
 <input type="text" required name="param_2" value="" placeholder="Parameter" class="request-input mb10">
 </div>
 </div>
 <div class="row">
 <div class="col-sm-12">
 <button class="btn btn-block button-orange">Get quotes now</button>
 </div>
 </div>
 </form> 

and this is the corresponding routes

Route::group([ 'middleware' => 'rental'], function(){
 Route::get('/my-requests/{readby_url}', 'PagesController@requests');
 Route::post('/request/{equipment_url}', 'PagesController@request');
 Route::post('/request/create', 'RequestsController@create');
 Route::post('/request/accept', 'RequestsController@accept');
});

My issue is with Route::post('/request/{equipment_url}', 'PagesController@request'); as it seems to not accept url parameters when the method is set to post.

i.e it throws the error

MethodNotAllowedHttpException in RouteCollection.php line 201:
in RouteCollection.php line 201
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 188
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 140
at RouteCollection->match(object(Request)) in Router.php line 746
at Router->findRoute(object(Request)) in Router.php line 655
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 237

I want to pass a parameter and post data at the same time.

Is there a way to make this work? I've been told that Route::post handles GET as well but it doesn't seem to work.

asked Aug 7, 2015 at 7:54
0

3 Answers 3

13

The problem has nothing to do with Laravel

<form url="/request/{{$equipment->url}}" method="POST">

replace url with action

<form action="/request/{{$equipment->url}}" method="POST">
answered Aug 7, 2015 at 8:42
Sign up to request clarification or add additional context in comments.

3 Comments

What are the odds that Jad answered Jad the same day this question was posted
A jadzillion to 1.
what are the odds that I read this comment and upvoted it and then came back a year later and tried to upvote it again, only to realise the arrow is orange because I already upvoted it
5

The HTTP POST verb doesn't accept parameters from the URL like GET, it accepts them from the Body of the HTTP POST. To fetch the post parameters you use the code below:

In routes.php:

Route::post('/request', 'PagesController@request');

and in your PagesController access the form input using one of the input methods such as below

public function request() 
{
 return Input::get('equipment_url');
}
answered Aug 7, 2015 at 8:17

4 Comments

The thing is I want the url to change and show the equipment_url, that's why I'm using route parameters
@JadSalhani then you will have to use GET
What's the motivation for this? In my case, passing parameter as a part of URL would make things much more convenient :(
I think this answer is missing the point. Yes, POST doesn't accept QUERY STRING params, but it does accept URI segments, and that's what the routes the OP posted are using.
5

You can not send get parameters to post route.

But you can achieve that by a simple trick, just pass your value ({{$equipment->url}}) in form's hidden filed or in session.

For Example:

html

<form url="test/{{$equipment->url}}" method="POST">
 {{Input::hidden('name-of-field', $equipment->url)
 <div class="row">
 .......
 </div>
</form>

route

Route::post('test/{any-variable}', ['as' => 'test', 'uses' => 'TestController@test']);

controller

public function test()
{
 echo "<pre>";
 dd(Input::all());
}

result

array(1) {
 ["name-of-field"]=>
 string(5) "your value here"
 }
answered Aug 7, 2015 at 8:41

1 Comment

I think this answer is missing the point, too. You can not send get parameters (QUERY STRING) with a POST, but the routes the OP posted are using URI segments. Laravel resolves them nicely so I don't see any problem.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.