How to redirect to a POST route in Laravel

Viewed 31985

In my controller, I have a method which should redirect to a POST route. I can not make this route a GET one either. Is there any solutions?

My controller code looks like:

// After a bunch of other if statements, etc
return redirect('newUser')->with('user', $user);

Now my route (in web.php) is as follows:

Route::post('/newUser', function() {
    $user=session('user');
    dd($user);
    return view('profileNewUser', $user);
});

The error which is thrown at me is MethodNotAllowedHTTPException.

I know my error is something to do with the _token, is there anyway I can allow the token field to get passed on? I tried passing it in with the with in my redirect but that doesn't work either.

Thanks for your help.

4 Answers

As far as i can see you can only redirect to a post route by using:

return back()->withInput(['postvar1' => 'postval1']);  // L5.5

This is used under the circumstance when there is an error in a form request.

Try going to the middleware folder and finding the VerifyCsrfToken file. aap/Http/Middleware/VerifyCsrfToken.php. Then add the route name you need to exempt from csrf token validation

Related