laravel asks for routes though the route exist - php

I have RestaurantController with these methods:
save
show($id)
when I finish executing the save method, I want to redirect the user to the show($id) method.
I tried this:
return Redirect::route('show', array($restaurant->id));
but I got :
InvalidArgumentException
Route [/show] not defined.
I also tried this:
NotFoundHttpException
though the show method exists.
in my routes.php I have:
Route::resource('restaurants', 'RestaurantsController');
could you help please?

Route is the name of the route, so in this instance it'd be:
return Redirect::route('restaurants.show', [$restaurant->id]);
See here for more information regarding redirects.
Also, just fyi, from the command line you can run php artisan routes to see a full list of routes and their corresponding names.

Related

Route in Laravel doesnt work on get Methode

I have tried to create a route with GET methode and doesnt work !!!
the route i would like to see on browser bar
http://*******:8000/products/categories/index
My controller
public function index(Request $request)
{
$categories = Category::where('parent_id', null)->orderby('name', 'asc')->get();
return view('products.categories.allcategories', compact('categories'));
}
My View folder for catategories under products
views
products
categories
My Route file
Route::get('products/categories', [CategoryController::class, 'index'])->name('index');
when type php artisan route:list i have my route listed but when i try to get the category page it show 404 not found :
http://********:8000/products/categories
if i change the route to this and controller index function to list then it works
Route::get('products/categories/list', [CategoryController::class, 'index'])->name('index');
any idea where i made error !
Your products/{product} route is conflicting with the products/categories one; it's trying to find a product matching categories.
This can typically be resolved by reordering the two routes in the routes file, or adding a ->where('^[0-9]+$') constraint to the products/{product} route if {product} is supposed to be numeric.
you should try php artisan route:cache
i use that code after add new route always and it's work
You're going to the wrong url you need to go to
localhost:8000/products/categories
The index method does not relate to the URL but the action.

Why does my edit & delete methods return a "404 not found" when my index & create routes work

Laravel version has updated and the routes is now expecting an object instead of an id from when i last used it.
My Routes:
When I try to pass over the $item object which the method in the controller wants. I get a 404 not found and my logs aren't returning... meaning the function isn't running. When the $item obj is not passed over the function realizes that a parameter is missing thus the method is recognized by the blade as being the same as the one in the controller.
Calling the edit function in Blade:
Controller Code:
I appreciate any help whatsoever.
The order of your routes is probably wrong
when you first define the show route with /item/{item} and then create with /item/create laravel will think the "create" is the id (or reference)
best way is to have
the index
create
....
show
Code Example correct
Route::get('/', ProductIndex::class)->name('product.index');
Route::get('/new', ProductCreate::class)->name('product.create');
Route::get('/{product}', ProductShow::class)->name('product.show');
Code Example wrong
Route::get('/', ProductIndex::class)->name('product.index');
Route::get('/{product}', ProductShow::class)->name('product.show');
Route::get('/new', ProductCreate::class)->name('product.create');

laravel making its own route to blade file

working a project with Laravel 5.6.
the problem is, when im going to an url like:
/login
or any other route and specify where it should go, it making its own route and going to another place. no metter if i even clear the code of that blade file.
i have not several routes or blade file that are the same. i have cleared my browser cache, laravel cach, config cache, and the command:
php artisan route:cache
did not worked to clear route cache.
my code example: web.php code
Route::get("/login", "LoginController#login");
Example: LoginController.php code
public function login()
{
return view('/login'); // not going to this path
}
to conclude, it does not read my code :(
need your ideas!
public function login()
{
return view('login');
}
view accepts the view name not the path of the route, If you want go to any route use redirect("/route_name") . But in your case if you redirect to login route again it will throw exception because the login route again calls this function. so you need to pass the view name. for example:
if your login view is in
resources
- views
-login.blade.php
then use above code. or if the login page is in any other folder inside view
it will be like return view("foldername.login")

Laravel routes not working inside a prefix unless given a variable

I'm fairly new to Laravel. I'm having a problem with routing.
Route::group(['prefix'=>'api/v1'],function(){
Route::resource('results','RequestController');
Route::get('results/getByName/{name}','RequestController#getByName');
Route::get('results/getLastTen','RequestController#getLastTen');
});
The problem is that the last route under prefix api/v1 does not work. When I call it it shows nothing, not even any error.
The code at the requestController is:
public function getLastTen(){
$results=DB::table('latest_random_trends')->limit(10)->get();
return $results;
}
Everything is alright with the code on the controller since it works when I call it from the routes.php file outside of the prefix 'api/v1' like this:
Route::get('results/getLastTen','RequestController#getLastTen');
but when it is inside the prefix it does not work unless I add a variable to it like this:
Route::get('results/getLastTen/{var}','RequestController#getLastTen');
Since you have a Route::resource above it, I think what's happening is that the show method on Resource Controller is getting the route instead of the one you wrote.
Try one the following:
Exclude the show method if you're not going to use it
Route::resource('results','RequestController', ['except' => 'show']);
Move your custom route above the resource route
Route::group(['prefix'=>'api/v1'],function(){
Route::get('results/getLastTen','RequestController#getLastTen');
Route::resource('results','RequestController');
Route::get('results/getByName/{name}', 'RequestController#getByName');
});
For more information, check out the show action on Laravel Docs

Laravel 5.4 function index try using function show

I have route:
Route::resource('admin/question', 'QuestionsController');
and function index:
public function index() {
return "Hello";
}
But when I try used index Laravel returned me the error:
Method [show] does not exist.
I'm using the link:
http://localhost:8012/siwz/siwz/public/admin/question
The server is WampServer program.
I can only use index function when I change route file:
Route::get('admin/question/index', 'QuestionsController#index');
Route::resource('admin/question', 'QuestionsController');
In Laravel version 5.3 I did not have to do it, it was enough to use:
Route::resource('.../...', '...Controller');
Actually, the URL is going to the correct function. admin/question should go to index. admin/question/{question} is the route that goes to show.
Take a look here and check how Laravel create Resource routes:
https://laravel.com/docs/5.4/controllers#resource-controllers
Since that you didn't provide your full routes. I am guessing that the link you were accessing is going to the wrong controller. You should check the ordering of the routes. Maybe you are accessing something like this on your route,
Route::resource('admin','AdminController');
And the AdminController doesn't have a method show(). Thats why laravel return that error.
Here's you can do
Comment out the rest of routes except route that you are accessing.
Try to reorder the affected routes. Maybe Laravel is confused with your route.

Categories