How to pass parameter on a route?

Asked

Viewed 6,064 times

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.

Clicking on a link from a line on this composite page, data referring to the line should be displayed.

For this, it will be necessary that the link pass its value to a new search routine, in order to consult again the database to collect more information, since the list obtained initially brings only summary data.

This part is not working. Results in error:

Sorry, the page you are Looking for could not be found. Notfoundhttpexception in Routecollection.php line 161:

The Routes code for the page call is 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 controllerUsuarios.php page and triggers 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 Return, which displays the result of all database records.

The page viewUsuariosLista.php 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;
?>

Repeat, so far everything works 100%.

Now comes the error. By clicking on 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 route call to Define, passing to the variable 'name' the name of the user clicked. A 'route', as demonstrated high up and which I repeat here

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

does not work, causing the mentioned error.

The view page named 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>

2 answers

1


Mauro, I’m glad you were able to solve the problem with the help of @rray, but then I suggest naming your routes because it will be much better if you need to make some future changes, for example:

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

After a read on the Blade, is a hand on the wheel... Very practical and leaves code super clean.

On the link you would call the route passing the name as follows:

<a href="{{ route('UsuarioDetalhar', $u->TabUsuariosNome) }}">
  • What is the advantage of passing an array in the route setting? + 1 by {{ route(....) }}

  • Well, I pass array because there are 2 parameters, one to define the name and the other to define the controller@method. I don’t know if you can name the route and inform the controller without using an array.

  • Route::get('usuarioDetalhar/{nome}','controllerUsuarios@detalhar'); usually I see so, I asked why was curiosity even :)

  • I get it, it’s because we assume that we need to change the "/user" path to "/userView". We would have to change tbm in all views. Already with her named I would not need, because I would continue calling her by name, regardless of the cainho has changed. I think q agr could explain

  • Now yes :D, works as alias.

  • iiiiiiisso!! rs

Show 1 more comment

0

You should set the parameter in the url with a slider and not with query.

Change:

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

To:

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

If the method signature has no parameter, 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;
}
  • Actually, this eliminates the error of not finding the page, but I found that the variable 'name', which should be filled by clicking on this link, is coming empty to the 'user view pageDetailing.php'. (I would like to rectify what I have just written. I meant 'the name variable is coming empty to the 'controllerUsuarios.php' page, where there is the 'detail' method'.

  • @Maurosimoes puts the code of detalhar()

  • The code fragment of the 'detail' method is public Function detail (){ $name=Request::input('name'); $sentenca="SELECT * from usuarios INNER JOIN categorias ON usuarios.Tabfkusuarioscategoria = categorias.Tabcategoriasid WHERE Tabusuariosnome = '". $name ." '"; echo $sentenca; ?>

  • @Maurosimoes, do the following change method signature to public function detalhar($nome){ ... and comment on the line $nome = Request::input("nome");

  • Now the problem was solved, after his tip, but I think it resulted in another: SELECT * from usuarios INNER JOIN categorias ON usuarios.Tabfkusuarioscategoria = categorias.Tabasid WHERE Tabusuariosnome = 'nome=Mauro Simões'. I see that the query has my name as a search, mixed with the variable name.

  • @Maurosimoes when you do echo $nome what appears?

  • I think I solved the problem: On the page viewUsersList.php, in the line where there was echo "<td><a href='usuarioDecarve? name=". $u->Tabusuariosnome."'> <span class='glyphicon glyphicon-search'></span></a></td>"; echo "</tr>"; I switched to echo "<td><a href='usuarioDecarve/". $u->Tabusuariosnome."'><span class='glyphicon glyphicon-search'></span></a></td>"; echo "</tr>";

  • 1

    Anyway, to make it clear to everyone who needs help like me, the call worked when I took the code from the user piece? name= and replace it/

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.