Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dactex's avatar

Returning to model.index view from Controller update method is adding a URL parameter

I am in a Boilerplate model update method (entered via a PATCH request to the 'boilerplate.update' route).

<form action="{{ route('boilerplate.update',['id'=>$bp->id]) }}" method="POST">
    {{-- Patching Objectives; this could include adding some... --}}
    @method('PATCH')
    @csrf

Routes are defined with:

Route::resource('/boilerplate', 'BoilerplateController')->middleware('auth');
|        | POST      | boilerplate                        | boilerplate.store   | App\Http\Controllers\BoilerplateController@store                    | web,auth     |
|        | GET|HEAD  | boilerplate                        | boilerplate.index   | App\Http\Controllers\BoilerplateController@index                    | web,auth     |
|        | GET|HEAD  | boilerplate/create                 | boilerplate.create  | App\Http\Controllers\BoilerplateController@create                   | web,auth     |
|        | DELETE    | boilerplate/{boilerplate}          | boilerplate.destroy | App\Http\Controllers\BoilerplateController@destroy                  | web,auth     |
|        | PUT|PATCH | boilerplate/{boilerplate}          | boilerplate.update  | App\Http\Controllers\BoilerplateController@update                   | web,auth     |
|        | GET|HEAD  | boilerplate/{boilerplate}          | boilerplate.show    | App\Http\Controllers\BoilerplateController@show                     | web,auth     |
|        | GET|HEAD  | boilerplate/{boilerplate}/edit     | boilerplate.edit    | App\Http\Controllers\BoilerplateController@edit                     | web,auth     |

After successfully updating the model, I just:


$boilerplate->update(['title' => Str::title($request->title), 'content' => $request->content]);
return view('boilerplate.index');

This is working with the index view displayed, but the URL displayed in the browser is of the "show" form

https://mydomain.com/boilerplate/nn

rather than the expected

https://mydomain.com/boilerplate

Can anyone help me understand why this is happening?

0 likes
3 replies
dactex's avatar

Thanks @Haz_, that does the job. Changed:

return view('boilerplate.index');

to:

return redirect()->route('boilerplate.index');

Any idea why this was happening? I'd like to understand what was going on with returning the view with the id of the last edited item at the end of the URL.

My best guess is that returning a view from that Controller method was not appropriate, because the entire method is geared for a PATCH response and returning the view passed that "context" on to the view, whereas a redirect to the route ends the PATCH context and and initiates the proper "GET" context for the redirected route.

1 like
haztakki's avatar

Glad it helped. You are only rendering the view, on the show route as you say.

Please or to participate in this conversation.