[php] Laravel 5 route not defined, while it is?

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}', '[email protected]');

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?

This question is related to php laravel routes laravel-5

The answer is

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:

    'as' => 'user.preferences.update',
    'uses' => '[email protected]'

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 (the uses).

Then, when you open the form, you call the route:

{!! Form::model(Auth::user(), [
    'method' => 'PATCH',
    'route' => ['user.preferences.update', Auth::user()->id]
]) !!}

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. :)

Similar questions with php tag:

Similar questions with laravel tag:

Similar questions with routes tag:

Similar questions with laravel-5 tag: