1
votes

I have Laravel app with Vue on front end, and Vue calls update method from controller using PUT request.

Request works, model gets updated, but I have issue with redirecting as it is redirecting also as a PUT instead of simple GET?

public function update(MomentsValidationRequest $request, Project $project, Task $task, Moment $moment)
{
    foreach($request->materials as $material){
        $material_id_array[$material['id']] = ['quantity' => $material['quantity']];
    }

    $moment->update($request->all());

    if(isset($material_id_array))
        $moment->materials()->sync($material_id_array);

    return redirect()->back()->with(['alert-type' => 'success', 'message' => 'Moment updated!']);
}

So naturally, I am getting a method not allowed exception because it is redirecting to a route which is supposed to get a previous view only.

Route itself is fine, request method isn't.

For non-believers :)

enter image description here

Also a route:

enter image description here

3
Have you tried to define the redirect within the 'Routes' instead of within the Controller? - cchoe1
What do you mean, using new Laravel's Route::redirect()? - Norgul
You simply want to redirect them back after the controller finishes, correct? You most likely have a Route defined that sends a request to the controller specified here. Instead of defining a redirect within this controller, you can define a back() method as a return value for the route itself. Here is an example laravel.com/docs/5.5/redirects right under 'Creating Redirects'. - cchoe1
I don't feel closures are the way to go here. Also, I need to redirect to /task/{id} which is not possible to forward then on redirect route without complicating - Norgul

3 Answers

0
votes

I know this is a bit late. But incase anyone stumbles across this.

You state that you're using Vue in the front end. This would suggest that the put request is being made through an axios call.

I can't see this call, so this is only an assumption. But I believe the solution would be to return a json object instead of a response in the controller, and then redirect trigger a redirect from the Vue component itself.

In the controller:

Session::flash('alert-type', 'success');
Session::flash('message', 'Moment updated!');

return response()->json(true);

In the component:

axios.post('/moments', this.moment).then(() => {
    window.location.replace("moments");
});

I believe this is something to do with how axios handles patch requests, it seems to attempt to handle a redirect response automatically, I could be wrong though, so any reply is welcome to if there's a better explanation.

0
votes

You can use:

redirect()->back(303)->with(...)
-2
votes

No, redirection is made always with GET but you don't have such route defined. So you should create GET route that will do something with this.

It's possible only to redirect to GET routes.