skip to Main Content

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 !

3

Answers


  1. you should try php artisan route:cache
    i use that code after add new route always and it’s work

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search