skip to Main Content

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}', [AppHttpControllersAppController::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.

2

Answers


  1. <?php
    
    // Path: routes/web.php
    use AppHttpControllersAppController;
    
    Route::get('/chat/{id}', [AppController::class, 'chat'])->name('chat');
    
    // Path: AppController.php
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    use AppModelsChat;
    
    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
    
    Login or Signup to reply.
  2. 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

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