Laravel is not returning the 'as' name of a specific request.
The below two examples show the output for each case (one works fine, the other does not)
The route is defined as a "resource", shows up in the route list as "companies.update" and all of the other routes work fine (except for update). Why is the update request not returning the route name?
{!! Form::model($company, ['route' => ['companies.update', $company->id], 'method' => 'patch', 'class' => 'form-horizontal']) !!}
#include('companies.form')
{!! Form::close() !!}
("update" does not return the name)
(every other route name works)
Have a look at php artisan route:list again.
There is two entries for update. One for PUT and one for PATCH.
Maybe use the method PUT to see if it shows up (but both should work when you look at the HTML).
Related
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]) !!}
But I'm getting this error:
Route [/preferences/1] not defined
A similar error occurs when calling the route() helper directly:
route('/preferences/' . Auth::user()->id');
I think I'm misunderstanding the docs on this topic but 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:
Route::patch('/preferences/{id}',[
'as' => 'user.preferences.update',
'uses' => 'UserController#update'
]);
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. :)
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:
Route::any('official/form/reject-form', 'FormStatus#rejectForm')
->name('reject-form');
Route::any('official/form/accept-form', 'FormStatus#acceptForm')
->name('accept-form');
Changing one of the names solves the problem. Copy, pasting, & fatigue can lead you to this problem :).
If route is not defined, then check web.php routing file.
Route::get('/map', 'NavigationController#map')->name('map'); // note the name() method.
Then you can use this method in the views:
<a class="nav-link" href="{{ route('map') }}">{{ __('Map') }}</a>
PS: the __('Map') is to translate "Map" to the current language.
And the list of names for routes you can see with artisan command:
php artisan route:list
I'm using Laravel 5.7 and tried all of the above answers but nothing seemed to be hitting the spot.
For me, it was a rather simple fix by removing the cache files created by Laravel.
It seemed that my changes were not being reflected, and therefore my application wasn't seeing the routes.
A bit overkill, but I decided to reset all my cache at the same time using the following commands:
php artisan route:clear
php artisan view:clear
php artisan cache:clear
The main one here is the first command which will delete the bootstrap/cache/routes.php file.
The second command will remove the cached files for the views that are stored in the storage/framework/cache folder.
Finally, the last command will clear the application cache.
when you execute the command
php artisan route:list
You will see all your registered routes in there in table format .
Well there you see many columns like Method , URI , Name , Action .. etc.
So basically if you are using route() method that means it will accept only name column values and if you want to use URI column values you should go with url() method of laravel.
One more cause for this:
If the routes are overridden with the same URI (Unknowingly), it causes this error:
Eg:
Route::get('dashboard', ['uses' => 'SomeController#index', 'as' => 'my.dashboard']);
Route::get('dashboard/', ['uses' => 'SomeController#dashboard', 'as' => 'my.home_dashboard']);
In this case route 'my.dashboard' is invalidate as the both routes has same URI ('dashboard', 'dashboard/')
Solution: You should change the URI for either one
Eg:
Route::get('dashboard', ['uses' => 'SomeController#index', 'as' => 'my.dashboard']);
Route::get('home-dashboard', ['uses' => 'SomeController#dashboard', 'as' => 'my.home_dashboard']);
// See the URI changed for this 'home-dashboard'
Hope it helps some once.
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:
CATEGORIES
And web.php routes file is defined like this:
Route::view('admin', 'admin.index')->name('admin');
i had the same issue and find the solution lately.
you should check if your route is rather inside a route::group
like here:
Route::group(['prefix' => 'Auth', 'as' => 'Auth.', 'namespace' => 'Auth', 'middleware' => 'Auth']
if so you should use it in the view file. like here:
!! Form::model(Auth::user(), ['method' => 'PATCH', 'route' => 'Auth.preferences/' . Auth::user()->id]) !!}
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.
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
Route::resource('example', 'ExampleController');
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:
Route::controller('example', 'ExampleController');
Please note that the command
php artisan route:list
Or to get more filter down list
php artisan route:list | grep your_route|your_controller
the forth column tells you the names of routes that are registered (usually generated by Route::resource)
It's just a query form. I searched for this problem and tried the many solutions I found, but none of them solved this problem.
Laravel version: 5.4.23. Using Blade template engine.
My route:
Route::post('products/search', 'ProductsController#search');
My form:
{{Form::open(['url' => ['products/search']])}}
{{Form::text('search', $search)}}
{{Form::submit('Search')}}
{{Form::close()}}
My controller:
class ProductsController extends Controller
{
...
public function search(Request $request) {
dd('This point is never reached.');
return view(...
}
}
Notes:
I tried to change the route to Route::get, but not worked. Tried to change my form method to GET, as {{Form::open(['method' => 'GET', 'url' => ['products/search']])}}, with no success.
I don't know the framework but why is the url value in the form an array?
I would have thought you'd want something like
Form::open(['url' => 'products/search'])
According to the docs, the default form method is POST so you should keep your route as Route::post. If you want to change the method to GET (which I would recommend for a search action), simply use Route::get and
Form::open(['url' => 'products/search', 'method' => 'get'])
Seems you can also use named route names or even controller methods via the action property, ie
Form::open(['action' => 'ProductsController#search'])
I think you need to change in your blade file like:
{{Form::open(['url' => ['products/search']])}}
TO
{{Form::open(['url' => 'products/search'])}}
OR you can add route name in your route like :
Route::post('products/search', 'ProductsController#search')->name('products.search');
{{ Form::open(['route' => 'products.search', 'method' => 'post']) }}
Hope this help you
So the issue might be:
Solution1. You have another route defined on top of that, something like: Route::get('products/{wildcard}', 'Controller'} in that case make sure you put all the other routes under the one you're trying to get to work.
Solution2
Go ahead and delete the controller. Then run composer dump-autoload
Then run the command on command line php artisan make:controller UserController then paste profile method that you have.
Solution3 You might have cached the routes so do: php artisan route:clear
Solution 4. change url to
{{ Form::open(array('url' => 'foo/bar')) }}
//
{{ Form::close() }}
I have a following code
{!! Form::open(array('action' => 'clientController#create')); !!}
it shouts that clientController#create is not defined unless I put some code into routes.php (this is my solution)
Route::post('clientRegistration', ['uses' => 'clientController#create', 'as' => 'registration']);
Everything then works fine, but I am not sure if I have found the correct solution, do I have to create a route for every form that will be created? I don't know why Laravel could not find a controller method without registering it in routes.php file
When you are calling an action Laravel searches through it's stored route to handler associations. This is done in routes.php
If you insert
Route::post('clientRegistration', ['uses' => 'clientController#create', 'as' => 'registration']);
in your routes.php you can do any of the following
{!! Form::open(array('action' => 'registration')); !!}
{!! Form::open(array('action' => 'clientController#create')); !!}
If you only stick with
Route::post('clientRegistration', 'clientController#create');
you can still use
{!! Form::open(array('action' => 'clientController#create')); !!}
More information here: https://laravel.com/docs/5.2/routing#named-routes
No matter what, you will need to inform Laravel about your route in routes.php, because otherwise it will not know how to handle the request.
I suggest
Route::resource('client', 'ClientController');
Please read
https://laravel.com/docs/5.2/controllers#restful-naming-resource-routes
I am currently creating a website that, once the user arrives, they are greeted by a form with which they input their unique id and DoB. Upon entering the information and clicking submit, they are sent to the main form which has only a little information on it and the user must enter the rest. My problem arises when I try to submit the form as I keep getting the following error:
at RouteCollection->methodNotAllowed(array('POST', 'PATCH'))
Note: I do not want any variables in my routes. (ex: I want 'form/person' and not 'form/{person_id}'). Also, I have included only the relavent information regarding the errors.
gate.blade.php - (this is where the user enters their ID and date of birth):
{!! Form::open(array('action' => 'JurorsController#form', 'class' => 'form-inline')) !!}
form.blade.php - (this is the primary form the user must fill out and submit):
{!! Form::open(['url' => action('JurorsController#submit'), 'method' => 'PATCH', 'class' => 'form-inline']) !!}
routes.php:
Route::patch('jurors/form', 'JurorsController#submit');
Route::get('jurors', 'JurorsController#gate');
Route::post('jurors/form', 'JurorsController#form');
JurorController#submit
public function submit(FormSubmitRequest $request)
{
//never reaches this point nor executes submit... instead redirects to gate IF it doesn't return 'MethodNotAllowedHttpException' error.
dd($request);
}
The only time I managed to get it to not show me the 'MethodNotAllowedHttpException' exception, I instead got redirected to the gate.blade.php page. If you have any questions for me or need me to clerify on anything, leave me a comment and I will respond once I am able to.
Thanks.
It looks to me your problem is the url in our routes. You are repeating them.
Firstly I would recommend using named routes as it will give you a bit more definition between routes. I'd change your routes to
Route::put('jurors/submit',[
'as' => 'jurors.submit',
'uses' => 'JurorsController#submit'
]);
Route::get('jurors',[
'as' => 'jurors.gate',
'uses' => 'JurorsController#gate'
]);
Route::post('jurors/form', [
'as' => 'jurors.form',
'uses' => 'JurorsController#form'
]);
Also on your submit route why are you using a PATCH request. wouldn't you use a POST request with all the data in? If you do still need to use Patch then you should be using put instead in your routes.
Another way for for testing and debugging you could use any to see if it is your HTTP request which is causing the error for example
Route::any('jurors/submit',[
'as' => 'jurors.submit',
'uses' => 'JurorsController#submit'
]);
Also then you can use the name of your route in your form::open() for example
{!! Form::open(array('route' => 'jurors.form', 'class' => 'form-inline')) !!}
Hope this helps
Working on trying to get the following form's URL to populate properly. Been stumbling over this for some time so here for some help.
As you can see from the following code, I am opening the form - binding the model - and trying to set the URL dynamically. the full URL is something like {username}/account/cards/id so i need to pass it the username (which i would like to pass the authenticated user (as they would only have access to their own page) and the ID of the card they are trying to update.
{!! Form::model($card, ['method' => 'PATCH', 'action' => 'Account\CardsController#update', array(Auth::user()->username, $card->id) ]) !!}
Now this is all happening in blade (front end) so not 100% what i am doing wrong. I have tried action, url, route... I can not get anything to work for some reason. Error I am getting on this one specifically is a array to string error. But if I can't build an array how do i pass in multiple variables? So a bit confused here.
any help would be appreciated.
Thanks
Citti
This is an update to my previous answer. You can try this one:
{!! Form::model($card, ['method' => 'PATCH', 'action' => [ 'Account\CardsController#update', Auth::user()->username, $card->id] ]) !!}
You're just passing your route parameters as an option to the Form::model tag, not the route. Try:
{!! Form::model($card, ['method' => 'PATCH', 'action' => [ 'Account\CardsController#update', [Auth::user()->username, $card->id] ] ]) !!}
If you're still having trouble I would suggest you name your route and reference the named route in the action.
If I'm not mistaken, the action should be an array if you are passing variables through to the controller. Ex:
{!! Form::model($card, array('method' => 'PATCH', 'action' => array('Account\CardsController#update', Auth::user()->username, $card->id))) !!}
Not sure if you are using Larvel Collective HTML and Forms, but they are essentially the same as the Laravel version. This page: http://laravelcollective.com/docs/5.0/html#form-model-binding explains more about your particular use case.
Hope it helps.
P.S. Try adding:
{!! Form::hidden('_method', 'PATCH') !!}
...underneath the open tag instead of within it. This has to do with L5 method spoofing, and is generally necessary for anything that is not 'POST' or 'GET', I believe. (i.e., 'PUT', 'PATCH', and 'DELETE')