0
votes

I am refactoring my code and getting 404 Page not found error in one route. I tried every possible solution but no luck. My route is give below:

Route::prefix('admin')->group(function () {
    .... other routes
    
    Route::prefix('product')->group(function () {
        .... other routes

        Route::prefix('category')->group(function () {
            Route::get('/', function () {
                dd('check');
            });

            <!-- Route::get('/', 'ProductCategoryController@index')->name('product_category_index'); -->
           
            .... other routes

        });
    });
});

In debugbar I am getting the exception:

No query results for model [App\Product] category F:\bvend\bvend.web\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php#389 Illuminate\Database\Eloquent\ModelNotFoundException

I have no longer App\Category model in my code. Instead I have App\ProductCategory

I have no clue what the mistake is. Please help.

1
How are you trying to access this route?Remul
Do you have a route that looks something like this: admin/product/{product}? Looks like a route conflict to me, maybe try placing Route::prefix('category') directly after Route::prefix('product')Remul
This is for Class mismatch, run composer dump-autoload hope the problem will gonesta
Hey !! it worked like charm.. can you please tell me where was the conflict? it seems just changing the code order. thanks a lot @RemulWahidSherief

1 Answers

2
votes

The problem is that two routes are conflicting with each other.

Lets say you have the following two routes with the following order:

admin/product/{product}

admin/product/category

When you try to access admin/product/category you are actually accessing admin/product/{product} with category as the value for the route parameter {product}.

That is why you get the error No query results for model [App\Product] category, it is trying to search for a product with the id category.

Now if you change the order:

admin/product/category

admin/product/{product}

Now the route admin/product/category has a higher priority than admin/product/{product}, so you actually can access your desired route, instead of getting matched into the admin/product/{product} route.