Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

motinska94's avatar

Why is this giving me a 404 error

Here's my code;

web.php

Route::prefix('employee')->middleware('auth')->group(function (){
        Route::prefix('categories')->middleware('auth')->group(function (){
            Route::get('details/{$id}', 'App\Http\Controllers\CategoryController@showDetails')->name('categories.details');
}};

I also tried;

Route::get('details/{$id}',[ 'App\Http\Controllers\CategoryController', 'showDetails'])->name('categories.details');

my link :

<a href="{{ route('categories.details', $category->id) }}" class="btn btn-primary btn-sm">Details</a>

in my categorycontroller :

    public function showDetails($id){
        dd("helo");
}

Error :

404 | NOT FOUND

Route does show up on php artisan route:list

GET|HEAD employee/categories/details/{$id} ........... categories.details › CategoryController@showDetails

0 likes
8 replies
Shivamyadav's avatar

Remove the Anchor tag $category->id and then try .. It showing you the 404 error because of the $category variable because you have not passed the data into your views blade file through the controller..

1 like
motinska94's avatar

@Shivamyadav Thanks for your answer! I'm not sure I really understand what you meant by passing data into my blade through the controller. I'm getting $category from a foreach loop that's getting the categories directly from my model like :

Category::whereStoreId(0)->get() as $category

However I've tried removing the $category variable from my link's route and gave the id as string like

route('categories.details', '44')

it had the same exact result. It's already going to the page it needs to go (employee/categories/details/44), but right when it needs to get to my controller and run the method, it throws a 404. I think the bug might be in my route file.

Shivamyadav's avatar

@motinska94 Try this :) Your route code be like this

Route::get('employee/categories/details/{id}',[ 'App\Http\Controllers\CategoryController', 'showDetails'])->name('categories.details');

Your controller code add this line to add a model class in the controller use App\Models\YourmodelName;

    public function showDetails($id){
        $categories = YourModelsName::all();
		return view('viewName', compact('categories'));
}

blade code using foreach loop

<a href="'employee/categories/details/{$categories->id}"  class="btn btn-primary btn-sm">Details</a>

1 like
motinska94's avatar

@Shivamyadav Route code is exactly the same as mine, except mine gets 'employee/categories/' part from its parent prefixes.

Controller code is irrelevant since the url doesn't go to the controller, which is my main problem.

Blade code is basically the same as :

route('categories.details', $id)

both returns the same string.

However I did try these and result is still the same.

kokoshneta's avatar

What URL does the link have in the generated HTML? Does it point to [your_domain]/employee/categories/details/[id]? Is id an actual, existing ID of a category that exists in your database?

(That’s an odd URL, by the way. How do multiple categories relate to a seemingly single employee without an employee ID anywhere? You’d expect it to be /employees/[employee_id]/categories/[category_id].)

Have you tried php artisan optimize:clear to clear your cached routes and views, to make sure that it’s actually getting your updates to the route definitions?

MichalOravec's avatar
Level 75

Do you have $ in your route definition? If so remove it.

It has to be like this:

Route::get('details/{id}', 'App\Http\Controllers\CategoryController@showDetails')->name('categories.details');
2 likes

Please or to participate in this conversation.