Tommy001's avatar

Keep getting error MethodNotAllowedHttpException

In order to upload either a new image in a new post or changing image in an old post, I have 2 routes that points to the same controller function in my web.php:

Route::post('/upload/{id}', 'UploadController@upload');
Route::post('/upload', 'UploadController@upload');

I have this in my UploadController:

public function upload($id = false, Request $request){
    // code to upload an image to a new post OR to a post that already exists
}

this is the form action to upload image in a new post (which works fine):

<form class="bild" action="{{ URL::to('upload') }}" method="post" enctype="multipart/form-data">

this is the form action to upload image in an old post (which does not work):

<form class="bild" action="{{ action('UploadController@upload',$recept->id) }}" method="post" enctype="multipart/form-data">

Whenever I try to use the route upload/{id} i get the error MethodNotAllowedHttpException.

Can anyone see what I am doing wrong?

0 likes
11 replies
yassiNebeL's avatar

Hello @Tommy001 ,

Try to inverse the order of arguments within the upload function

public function upload(Request $request, $id = false)

then try to execute this command : php artisan route:list to see your routes.

But this is not a good way to accomplish it!!

because you could set the $id as an optional argument like this Route::post('/upload/{id?}', 'UploadController@upload');

Hope this help you (:

2 likes
rin4ik's avatar
<form class="bild" action="{{ route('upload',$recept->id) }}" method="post" enctype="multipart/form-data">

route

Route::post('/upload/{id?}', 'UploadController@upload')->name('upload');

id must be null by default if you pass id it will equal to that value

public function upload($id = null, Request $request){
    // code to upload an image to a new post OR to a post that already exists
}
1 like
Tommy001's avatar

yassiNebel, thanks for the tip on the optional argument in the route. I didn't know about that. Still have to state $id = false in the argument list though? Unfortunately this did not help. Same result.

rin4ik: thanks for the tip but I also have included the csrf token in all forms.

Tommy001's avatar

rin4ik: thanks again, I tried your suggestion to use a named route, but no luck. I must be doing something really stupid, unfortunately I cannot see what it is (spent hours on this now...)

yassiNebeL's avatar

You're welcome @Tommy001 ,

as you set the $id as an optional parameter, yes you will deal with it

public function upload(Request $request, $id = null)
{
....
}

but you should remove the 2nd route within the web.php.

Tommy001's avatar

yassiNebel, yes I have done so. No matter what I try I keep getting the Method not allowed error. It really should work now :-|

rin4ik's avatar
rin4ik
Best Answer
Level 50

try please

public function upload(Request $request, $id = null)
{
dd('something');
}

maybe you will try with capital letter method="post"

<form class="bild" action="{{ route('upload',$recept->id) }}" method="POST" enctype="multipart/form-data">
1 like
yassiNebeL's avatar

@Tommy001 don't worry I will help to fix this error :

here you go

$php artisan config:clear && php artisan cache:clear && php artisan route:clear && php artisan view:clear

Sometimes all you need is clearing your repo

1 like
Tommy001's avatar

rin4ik; !! I tried your suggestion to clear everything and just dump some text in the function and then it did find this route! So there is something wrong with my code inside the controlelr function. The error message made me think that it must be that the route is not found... One step closer to the solution. Thanks yassiNebel for the tip on clearing things also, I will do that too..

Tommy001's avatar

Found it. In the function I tried to do a redirect back to where I came from in stead of just returning the approriate view. So the message about "method not allowed" probably came from the next step AFTER my upload function. Thanks a lot for your help. I keep learning.

Please or to participate in this conversation.