Search code examples
phplaravellaravel-5routeshttpexception

Error in passing argument to a controller function in laravel


I am trying to pass a variable $id to Controller function in routes file.

Following are the snippets from the respective files:

Routes.php

Route::get('/news-post/{$id}', 'BlogController@getPost');

BlogController.php

public function getPost($id)
    {
            $post = \App\blog::find($id);
            if($post == NULL){
                App::abort(404);
            }
            return View('webpages.news-post',['title'=>$post['title'],'post'=>$post]);
    }

I am getting a NotFoundHttpException in RouteCollection.php line 161:

I have tried searching for the reason but unable to find any.


Solution

  • Change the route from

    Route::get('/news-post/{$id}', 'BlogController@getPost');
    

    to:

    Route::get('/news-post/{id}', 'BlogController@getPost');
    

    Remove the dollar sign before the id in the route. Check the example in the official documentation. Hope this helps.