Laravel route returns 404 (Laravel 6.6.2)

All we need is an easy explanation of the problem, so here it is.

I have multiple routes where every route works except one. I keep getting a 404 page even though I cleared the cache and also checked the list of the routes with

php artisan route:list

| GET|HEAD| team/requests/{$teamId} | team_requests | App\Http\Controllers\[email protected]                           | web                          |

The route appeared on the list.

My route looks like this

Route::get('team/requests/{$teamId}', '[email protected]')->name('team_requests');

My controller action looks like this:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TeamController extends Controller
{

    public function requests(int $teamId)
    {
        $requests = User_Team::where('team_id', $teamId)->where('isInvitation', false)->get();
        $userRequests = [];
        foreach ($requests as $request) {
            $userRequests[] = User::find($request->user_id);
        }

        return view('requests')->with('requests', $userRequests);
    }
}

I’m running the website currently with the laravel localhost server using:

php artisan serve

I think I must have a typo somewhere but I can’t find anything. Does someone also had this issue?

I’m using Laravel 6.6.2

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Thanks for the solution from lagbox.

The problem was that I was using $ in the defintion of the route parameter.
Removing it solved the problem.

Method 2

While declaring parameters in the route.php, no need to declare with ‘$’. Just simply, write by {id} or {teamid}, rest I think is OK.

Here is a documentation on how to add parameters to the URL in routes.

Also, I think, you should upgrade to Laravel 8, there are some cool features in it.

Though, you already got your solutions, hope this too helps. Thank You 🙂

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply