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.
Related
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');
I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.
I try to get my route without any additional parameter in url and i'm getting Sorry, the page you are looking for could not be found. error.
If I use url like: domain/category/lenovo where lenovo is my slug it's working. But if I try to get my url like: domain/lenovo I'm gettin error above.
here is my codes:
route
Route::get('/{categoryslug}', 'frontend\FrontendController#totalcategoriessubs')->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');
function
public function totalcategoriessubs($categoryslug) {
$categories = Category::where('slug','=',$categoryslug)->with('subcategories')->paginate(12);
return view('front.categoriessubs', compact('categories'));
}
it also brakes my other urls such as domain/admin/dashboard etc.
any idea?
It seems that is caused by the order of your routes.
Your actual problem is that when you hit a fixed endpoint, for example /dashboard this will be handled by Route::get('/{categoryslug}', ...) before the correct endpoint (Route::get('dashboard', ...)). So, as your system notices that there is no category with that slug (dashboard) it throws the error.
Try change the order of your routes, like this:
Route::get('/category/{categorySlug}', 'CategoryController#index');
Route::get('/dashboard', 'DashboardController#home');
// and so on..
Route::get('/{categoryslug}', 'frontend\FrontendController#totalcategoriessubs')
->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');
Always put the routes that have fixed parts (.../category/...)of the route before the ones that have dynamic elements (.../{categoryslug}/).
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
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.