Home > Enterprise >  Route exists but 404 Laravel 9
Route exists but 404 Laravel 9

Time:11-24

I have a problem where I want to pass a parameter in the url, in this case "id". But when I click on the url it returns a 404 error. The route exists and this is the code.

Web.php Route::get('/chat/{id}', [App\Http\Controllers\AppController::class, 'chat'])->name('chat');

home.blade.php a href="/chat/{{$chat['id']}}">test</a

AppController

return view('chat')

I did not put anything in the controller yet as i first wanted to test if it even links to the blade file.

I tried clearing the cache of the routes and renaming anything but still no positive result.

CodePudding user response:

<?php

// Path: routes/web.php
use App\Http\Controllers\AppController;

Route::get('/chat/{id}', [AppController::class, 'chat'])->name('chat');

// Path: AppController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Chat;

class AppController extends Controller
{
    public function chat($id)
    {
        $chat = Chat::find($id);
        return view('chat', compact('chat'));
    }
}

// Path: home.blade.php
@foreach ($chats as $chat)
    <a href="{{ route('chat', $chat->id) }}">{{ $chat->name }}</a>
@endforeach

CodePudding user response:

In your blade from where you are accessing the route change the code into this...

<a href="{{ route('chat', $chat->id) }}">test</a>

It should work like this. Even after that it won't work run:

php artisan route:clear

It will clear the route cache

  • Related