Laravel 5.1 - Routes Error - NotFoundHttpException in RouteCollection.php line 143: - laravel

This error happen in my laravel 5.1 project, when trying to access http://localhost:8000/dashboard.user.update after I click Submit on my edit form.
NotFoundHttpException in RouteCollection.php line 143:
This error happen when I run update function in my controller. I have no error when runing other function like index or show.
This is my routes:
...
// Dashboard routes...
Route::resource('dashboard/user', 'UserController');
Route::resource('dashboard', 'DashboardController');
...
I already runs php artisan route:list to see if my routes are working:
+--------+----------+-----------------------+------------------------+---------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-----------------------+------------------------+---------------------------------------------+------------+
...
| | POST | dashboard/user | dashboard.user.store | App\Http\Controllers\UserController#store | auth |
| | GET|HEAD | dashboard/user | dashboard.user.index | App\Http\Controllers\UserController#index | auth |
| | GET|HEAD | dashboard/user/create | dashboard.user.create | App\Http\Controllers\UserController#create | auth |
| | DELETE | dashboard/user/{user} | dashboard.user.destroy | App\Http\Controllers\UserController#destroy | auth |
| | GET|HEAD | dashboard/user/{user} | dashboard.user.show | App\Http\Controllers\UserController#show | auth |
| | PUT | dashboard/user/{user} | dashboard.user.update | App\Http\Controllers\UserController#update | auth |
| | PATCH | dashboard/user/{user} | | App\Http\Controllers\UserController#update | auth |
...
Now I don't know what to do, can help?

Like your routes table says you need PUT or PATCH for an update call. Your route is correct but laravel checks the routes also for the method type.
I think you want to display a view and not return a json (RestAPI) if so than the implicit controllers are much better than resources!
http://laravel.com/docs/5.1/controllers#implicit-controllers
If you want to use them you have to rename your methods to getIndex(), getEdit(), postStore() and if you want post not put than postUpdate().

If it's an update, tell laravel it's a "PUT" request. Since simply POST method for that url with the user_id not exists.
<input name="_method" type="hidden" value="PUT">
Something like this:
<form method="POST" action="http://localhost:8000/dashboard/user/1">
<input name="_method" type="hidden" value="PUT">
//...

Related

Laravel Route not defined error on FP_route

I'm having a problem with a POST, when I try to run the page and data is loaded in the database table I'm trying to read I always get a Route [MAILFP_Tree] not defined error.
The code is triggered by a call for the route in the blade file that will visualize the page
<form action="{{route('MAILFP_Tree')}}" method="post">
the routes are the following
Route::get('FP_Tree', 'HomeController#toFP_Tree')->middleware('auth');
Route::post('FP_Tree', 'MailController#toFP_Tree')->middleware('auth')->name('MAILFP_Tree');
This is the controller created for the post method (it's the one called mailController)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \Illuminate\Support\Facades\Mail;
use App\Mail\emailFP_TreeRow;
use DB;
use Auth;
use App\AssociationHistorical;
class MailController extends Controller
{
public function toFP_Tree(Request $request)
{
$order_number = $request->input('order_number');
$customer_name = $request->input('customer_name');
$date_order_placed = $request->input('date_order_placed');
$item_number = $request->input('item_number');
$quantity = $request->input('quantity');
$item_description = $request->input('item_description');
$passed = array($order_number, $customer_name, $date_order_placed, $item_number, $quantity, $item_description);
Mail::to(Auth::user()->email)->send(new emailFP_TreeRow($passed));
//passes data to blade
$rows = AssociationHistorical::all();
return view('FP_Tree',[
'rows'=>$rows
]);
}
}
this is the content of the homecontroller
public function toFP_Tree()
{
//passes data to blade
$rows = AssociationHistorical::all();
return view('FP_Tree',[
'rows'=>$rows
]);
}
This is my migration table
Schema::create('association_historical', function (Blueprint $table) {
$table->id();
$table->string('order_number');
$table->string('item_number');
$table->string('item_description');
$table->date('date_order_placed');
$table->string('customer_name')->nullable();
$table->string('sales_rep_email');
});
The thing is that the problem happens when there is data in the association_historical table, most likely It is related to the fact that if there is no data in the database it won't run the post method, but i thought that it would make sense to show the database table...
the strange thing is that on someone else computer this works without any problem.
This is the result from PHP artisan route:list
+--------+----------+--------------------+-----------------------------+----------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+--------------------+-----------------------------+----------------------------------------------------------+------------+
| | GET|HEAD | / | generated::UYk0LlF0PlICNJsK | App\Http\Controllers\LoginPageController#showLogin | web |
| | POST | / | login | App\Http\Controllers\LoginPageController#doLogin | web |
| | GET|HEAD | FP_Tree | generated::98bieUMU1hfvx1TH | App\Http\Controllers\HomeController#toFP_Tree | web |
| | | | | | auth |
| | GET|HEAD | Prediction | generated::4HdessOBvQL3KmpT | App\Http\Controllers\HomeController#toPrediction | web |
| | | | | | auth |
| | GET|HEAD | PredictionsLanding | generated::Xtf7I1MCKtlOSX3Y | App\Http\Controllers\HomeController#toPredictionsLanding | web |
| | | | | | auth |
| | GET|HEAD | UserManagement | generated::aOYFVG9JOLTfGjB5 | App\Http\Controllers\HomeController#toUserManagement | web |
| | | | | | auth |
| | GET|HEAD | api/user | generated::3SA9mFdg4RMncH1a | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | logout | generated::YeCPB1fIxlH8JqDe | App\Http\Controllers\HomeController#doLogout | web |
| | | | | | auth |
+--------+----------+--------------------+-----------------------------+----------------------------------------------------------+------------+
it was so simple, thanks DigitalDrifter, just had to do a php artisan route:clear

Route::has note detecting route resource

I am trying to create links in the top nav menu using the code to check if the route name has a text pattern in it.
All is working except for one route which is also defined as a resource.
Below is my code in the web.php file
Route::get('/bookings', 'BookingController#index')->name('bookings');
Route::resource('/bookings', 'BookingController');
In the app.blade.php file I have the code:
#if (Route::has('bookings'))
<li class="nav-item">
<a class="nav-link" href="{{ route('bookings') }}">{{ __('Bookings') }}</a>
</li>
#endif
I checked the routes in php artisan and its listed as existing.
| api,auth:api |
| | POST | bookings | bookings.store | App\Http\Controllers\BookingController#store | web |
| | GET|HEAD | bookings | bookings.index | App\Http\Controllers\BookingController#index | web |
| | GET|HEAD | bookings/create | bookings.create | App\Http\Controllers\BookingController#create | web |
| | PUT|PATCH | bookings/{booking} | bookings.update | App\Http\Controllers\BookingController#update | web |
| | GET|HEAD | bookings/{booking} | bookings.show | App\Http\Controllers\BookingController#show | web |
| | DELETE | bookings/{booking} | bookings.destroy | App\Http\Controllers\BookingController#destroy | web |
| | GET|HEAD | bookings/{booking}/edit | bookings.edit | App\Http\Controllers\BookingController#edit
Route::has('bookings') checks the route list for a route named bookings. While you've got a URL of bookings, its name in the route list is bookings.index - you don't have any route named bookings.
(Per your php artisan route:list output, your route from the Route::resource call is wiping out the earlier Route::get call. Remove that redundant/ignored Route::get definition.)
Route::has('bookings.index') should do the trick (and changing route('bookings') to route('bookings.index').

Laravel 5.6 php artisan route:list shwoing only api middleware why?

I am trying to make api from laravel5.6, its works but both are not wotrking, I am not able to run website, because web route not working
in route list shpwing on api middleware why?
$ php artisan route:list
+--------+----------------------------------------+--------------------+----------+--------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------------------------------------+--------------------+----------+--------------------------------------------------+------------+
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | api/en/v1/gettoken | gettoken | App\Http\Controllers\Api\ApiController#gettoken | api |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | api/en/v1/login | login | App\Http\Controllers\Api\UserController#login | api |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | api/en/v1/register | register | App\Http\Controllers\Api\UserController#register | api |
Write your routes in routes/web.php. all routes in api.php are uses the api middleware.

Laravel 5.2 : MethodNotAllowedException

I've defined the following route in Laravel:
Route::group(['prefix' => 'api'], function() {
Route::post('login', [
'uses' => 'Auth\AuthController#login',
'as' => 'auth.login',
]);
});
And I'm using Postman to send a request like this (you can also see the results):
Why am I getting a MethodNotAllowed exception????
I also tried creating a form in an empty html file, with the method set to post. but got the same results.
EDIT
If i add a route::get that shows a login form, after the post request in Postman it shows that login form.
EDIT 2:
output of php artisan route:list for our route entries:
+--------+----------+--------------+---------------------+----------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+--------------+---------------------+----------------------------------------------------+------------+
| | GET|HEAD | / | guest.home | App\Http\Controllers\GuestController#index | |
| | GET|HEAD | a/dashboard | admin.dashboard | Closure | |
| | POST | api/login | auth.login | App\Http\Controllers\Auth\AuthController#login | |
| | GET|HEAD | api/login | auth.login | Closure | |
| | GET|HEAD | api/logout | auth.logout | App\Http\Controllers\Auth\AuthController#getLogout | jwt.auth |
| | POST | api/register | auth.register | App\Http\Controllers\Auth\AuthController#register | jwt.auth |
| | GET|HEAD | m/dashboard | moderator.dashboard | Closure | |
| | GET|HEAD | pu/dashboard | premium.dashboard | Closure | |
| | GET|HEAD | u/dashboard | user.dashboard | Closure | |
+--------+----------+--------------+---------------------+----------------------------------------------------+------------+
EDIT3
One more curious thing. If i set the method to Route::any, I get rid of the exception, but then I can't access the post parameters. i.e. I don't have any post parameters.
EDIT 4:
If I add a route::get and show the login view there and send the login credential, it works. But not in Postman.
Use x-www-form-urlencoded instead of form-data in postman, See the difference below.
form-data
multipart/form-data is the default encoding a web form uses to transfer data. This simulates filling a form on a website, and submitting it. The form-data editor lets you set key/value pairs (using the key-value editor) for your data. You can attach files to a key as well. Do note that due to restrictions of the HTML5 spec, files are not stored in history or collections. You would have to select the file again at the time of sending a request.
urlencoded
This encoding is the same as the one used in URL parameters. You just need to enter key/value pairs and Postman will encode the keys and values properly. Note that you can not upload files through this encoding mode. There might be some confusion between form-data and urlencoded so make sure to check with your API first.
Unfortunately the problem was with Postman3. I'm using Advanced REST Client now, and it works alright. Postman would send GET requests no matter what method I chose.

Laravel not able to route to controller

Here is my controller class:
class CallrequestController extends BaseController {
public function getIndex()
{
return View::make('results');
}
}
which is saved in:
--app
--controllers
--CallrequestController.php
then I autoloaded via composer:
composer dump-autoload
then i add the route to route file:
Route::controller('getrequest','CallrequestController');
here is the registered route via php artisan routes:
+--------+-------------------------------------------------------------------+------+-------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+-------------------------------------------------------------------+------+-------------------------------------+----------------+---------------+
| | GET|HEAD / | | Closure | | |
| | GET|HEAD callrequest/index/{one?}/{two?}/{three?}/{four?}/{five?} | | CallrequestController#getIndex | | |
| | GET|HEAD callrequest | | CallrequestController#getIndex | | |
| | GET|HEAD|POST|PUT|PATCH|DELETE callrequest/{_missing} | | CallrequestController#missingMethod | | |
+--------+-------------------------------------------------------------------+------+-------------------------------------+----------------+---------------+
yet its not routing to the controller, i get the default "Whoops, looks like something went wrong." error message.
what am i doing wrong?
edit also here is a sample url:
mydomainname.com/getrequest

Resources