Theo Theo - 1 month ago 10
PHP Question

Passing variables into views

I am a new learner of Laravel,and currently I am playing with views. However,after passing a variable in my view,I get this exception. Basically it's saying that the route doesn't exist.

NotFoundHttpException in RouteCollection.php line 161:
in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 821
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in
CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'),
array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54


So here is what I am doing

routes.php

Route::get('show_comment/{$id}','CommentsController@showComment');


CommentsController.php

class CommentsController extends Controller
{

public function index()
{

}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return "inside Create method";
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return "Show method:" .$id;
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}


public function destroy($id)
{
//

}

public function contact(){
return view('contacts');
}


public function showComment($id){
return view('show_comment') -> with('id',$id);
}

}


And finally I have my simple view.

<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>

<link href="https://fonts.googleapis.com/css?family=Lato:100"
rel="stylesheet" type="text/css">

</head>
<body>
<div class="container">
<h1/>Show comment {{$id}}}</h1>
</div>
</body>
</html>


What am I missing?

Thanks,

Theo.

Answer

As @Xatenev said in the comments, it should just be a case of removing $ in the route so changing:

Route::get('show_comment/{$id}','CommentsController@showComment');

to:

Route::get('show_comment/{id}','CommentsController@showComment');