I'm a little confused on how this is supposed to work. But I'm getting an Route [/preferences/1] not defined
error.
In my routes.php I have:
Route::patch('/preferences/{id}', 'UserController@update');
And in the view file (account/preferences.blade.php) I have:
{!! Form::model(Auth::user(), ['method' => 'PATCH', 'route' => '/preferences/' . Auth::user()->id]) !!}
I'm getting an error telling me the route doesn't exist. I think I'm misunderstanding the docs on this topic but in my opinion I've defined a route for PATCH requests with a given parameter, and set this in the view correctly.
What am I overlooking here?
The
route()
method, which is called when you do['route' => 'someroute']
in a form opening, wants what's called a named route. You give a route a name like this:That is, you make the second argument of the route into an array, where you specify both the route name (the
as
), and also what to do when the route is hit (theuses
).Then, when you open the form, you call the route:
Now, for a route without parameters, you could just do
'route' => 'routename'
, but since you have a parameter, you make an array instead and supply the parameters in order.All that said, since you appear to be updating the current user's preferences, I would advise you to let the handling controller check the id of the currently logged-in user, and base the updating on that - there's no need to send in the id in the url and the route unless your users should need to update the preferences of other users as well. :)
If route is not defined, then check web.php routing file.
Then you can use this method in the views:
PS: the __('Map') is to translate "Map" to the current language.
And the list of names for routes you can see with artisan command:
On a side note:
I had the similar issues where many times I get the error Action method not found, but clearly it is define in controller.
The issue is not in controller, but rather how routes.php file is setup
Lets say you have Controller class set as a resource in route.php file
then '/example' will have all RESTful Resource listed here: http://laravel.com/docs/5.0/controllers#restful-resource-controllers
but now you want to have some definition in form e.g: 'action'=>'ExampleController@postStore' then you have to change this route (in route.php file) to:
This thread is old but was the first one to come up so i thought id share my solution too. Apart from having named routes in your routes.php file. This error can also occur when you have duplicate URLs in your routes file, but with different names the error can be misleading in this scenario. Example
Changing on of the names solves the problem. Copy pasting and fatigue will get you to this problem :).
My case is a bit different, since it is not a form but to return a view. Add method
->name('route')
.MyView.blade.php
looks like this:And
web.php
routes file is defined like this:In my case the solution was simple:
I have defined the route at the very start of the
route.php
file.After moving the named route to the bottom, my app finally saw it. It means that somehow the route was defined too early.