Route::has note detecting route resource - laravel

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

Related

Getting same page on every URL/Route in Laravel8

I'm using default laravel auth , I have multiple routes with prefix group but when I use other routes/urls but giving the same page on every route which is View page the product
you can see my route
Auth::routes();
Route::prefix('products')->group(function () {
Route::get('/', [ProductsController::class, 'index']);
Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');
});
On this below route giving the product_view page which I can't understand why is giving
Route::get('/test_route', , function () {
return 'Hello World';
});
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
| Domain | Method | URI | Name | Action
| Middleware |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
| | GET|HEAD | api/user | generated::suiIeX6oHZWSnSP1 | Closure
| api |
| | | | |
| App\Http\Middleware\Authenticate:sanctum |
| | GET|HEAD | sanctum/csrf-cookie | generated::WKOquUKjM8nfHvKl | Laravel\Sanctum\Http\Controllers\CsrfCookieController#s
how | web |
| | GET|HEAD | products | generated::IucNuoXqqYtZPi70 | App\Http\Controllers\ProductsController#index
| web | |
| | GET|HEAD | {code} | products.view_product | App\Http\Controllers\ProductsController#view_product
| web |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+

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 returning Error 404 but the route exists

I have a blade template where I call a route to the show method:
<div class="user"><div><img src="{{asset('images/user.svg')}}" alt=""></div></div>
That route is controlled by a resource controller and the routes are set like this:
Route::resource('profile', 'App\Http\Controllers\ProfileController')->middleware('auth');
All the routes of that controller are defined as you can see from the output of my php artisan route:list:
| | GET|HEAD | profile | profile.index | App\Http\Controllers\ProfileController#index | web |
| | | | | | auth |
| | POST | profile | profile.store | App\Http\Controllers\ProfileController#store | web |
| | | | | | auth |
| | GET|HEAD | profile/create | profile.create | App\Http\Controllers\ProfileController#create | web |
| | | | | | auth |
| | GET|HEAD | profile/{profile} | profile.show | App\Http\Controllers\ProfileController#show | web |
| | | | | | auth |
| | DELETE | profile/{profile} | profile.destroy | App\Http\Controllers\ProfileController#destroy | web |
| | | | | | auth |
| | PUT|PATCH | profile/{profile} | profile.update | App\Http\Controllers\ProfileController#update | web |
| | | | | | auth |
| | GET|HEAD | profile/{profile}/edit | profile.edit | App\Http\Controllers\ProfileController#edit | web |
| | | | | | auth |
So far the controller only has a dd function since I haven't started writing it yet and the show method is the only one with anything written yet:
public function show($id)
{ dd($id);}
But for some reason, whenever I try acessing that route, it returns a 404 Not Found error. I tried accessing the index method and it works, but the show method always returns the 404 Not Found error. I also tried php artisan route:clear but it didn't solve the problem. What could I be doing wrong here?
Try out it , list all of router
php artisan route:list
Or you can as such that
Route::resource('profile', 'ProfileController', [
'names' => [
'index' => 'show.show'
'store' => 'profile.new',
]
]);
And if you try this ?
<div class="user"><div><img src="{{asset('images/user.svg')}}" alt=""></div></div>

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

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">
//...

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