How to pass parameter in a route?

2

I have a simple test routine, which is not working in the final part. The idea is to query a MySql database and compose a page with the result, which works correctly.

By clicking on a link in a line of this composite page, line data should be displayed.

To do this, the link must pass its value to a new search routine, in order to query the database again to collect more information, since the list obtained initially only contains summarized data.

This part does not work. It results in error:

  

Sorry, the page you are looking for could not be found.   NotFoundHttpException in RouteCollection.php line 161:

The route code for page calls looks like this:

<?php
Route::get('/', function () {
    return view('welcome');
});
Route::get('usuarioLer','controllerUsuarios@pesquisar');//pesquisa todo o BD

Route::get('usuarioDetalhar/{nome}','controllerUsuarios@detalhar');//pesquisa um item no BD

Route::group(['middleware' => ['web']], function () {
});
?>

The 'route' userLer is the one that works 100%. This route, as noted, calls the page controllerUsuarios.php and causes the search method.

A fragment of this php page is:

public function pesquisar (){
    $listaUsuarios = DB::select
 ("SELECT * from usuarios INNER JOIN categorias ON 
usuarios.TabFKUsuariosCategoria = categorias.TabCategoriasID");
    return view('viewUsuariosLista')->with('listaU',$listaUsuarios);
}//metodo pesquisar

The above code calls a 'view' page through the return, which displays the result of all the records in the database.

The viewUsuariosLista.php page has the following code:

<?php
echo <<<BLOCO1
<html>
<head>
    <!-- <link href="../public/css/app.css" rel="stylesheet"> -->

    <link href="assets/css/bootstrap.min.css" rel="stylesheet">         
    <script src="assets/js/bootstrap.min.js"></script>

 </head>
<body>
<table class="table">
BLOCO1;
foreach ($listaU as $u){
echo "<tr>";
echo    "<td>".$u->TabUsuariosID."</td>";
echo    "<td>".$u->TabUsuariosNome."</td>";
echo    "<td>".$u->TabCategoriasDescricao."</td>";
echo    "<td><a href='usuarioDetalhar?nome=".$u->TabUsuariosNome."'>
<span     class='glyphicon glyphicon-search'></span></a></td>";
echo "</tr>";
}//foreach
echo <<<BLOCO2
</table>
<br />
</body>
<html>
BLOCO2;
?>

Repeating, so far everything works 100%.

Now, come the error. By clicking the link generated by the line

echo    "<td><a href='usuarioDetalhar?nome=".$u->TabUsuariosNome."'>
<span     class='glyphicon glyphicon-search'></span></a></td>"

It causes the user's 'route' call to be specified, passing the 'name' variable to the name of the user clicked. The 'route', as shown above and here I repeat

Route::get('usuarioDetalhar/{nome}','controllerUsuarios@detalhar');//pesquisa um item no BD

does not work, causing the mentioned error.

The view 'view' page viewUsuariosDetalhes.php exists, and its simple code is

<html>
<head>
</head>
<body>
<h1>Detalhes do Usuário <br />
<h3><?php echo($u->TabUsuariosNome);?></h3>
</h1>
</h1>
<ul>
<li>
<b>Categoria:</b><?php echo($u->TabCategoriasDescricao); ?>
</li>
</ul>
</body>
</html>
    
asked by anonymous 02.02.2016 / 11:49

2 answers

1

Mauro, how nice that you could solve the problem with the help of @rray, but I suggest naming your routes later, because it will be much better if you need to make any future changes, for example:

Route::get('usuarioDetalhar/{nome}',['as' => 'UsuarioDetalhar', 'uses' => 'controllerUsuarios@detalhar']);//pesquisa um item no BD

After one read about the Blade , it's a hand in the wheel ... Very practical and leaves the code super clean.

In the link you would call the route by passing the name as follows:

<a href="{{ route('UsuarioDetalhar', $u->TabUsuariosNome) }}">
    
02.02.2016 / 12:53
0

You should set the parameter in the url with a slash and not with a question mark.

Change:

echo "<td><a href='usuarioDetalhar?nome=".$u->TabUsuariosNome."'>

To:

echo "<td><a href='usuarioDetalhar/".$u->TabUsuariosNome."'>

If the method signature has no parameters, set one to receive the value.

public function detalhar ($nome){ 
    $sentenca="SELECT * from usuarios INNER JOIN categorias ON usuarios.TabFKUsuariosCategoria = categorias.TabCategoriasID WHERE TabUsuariosNome = '".$nome ."'";
    echo $sentenca;
}
    
02.02.2016 / 12:19