Return database search with Controller

Asked

Viewed 261 times

1

I’m trying to take the content name of an input from my search page (Search), and send it to another view (Data), where the controller will receive this value and from there will search the database for information related to this name (Ex: id, name or gender)so it will fill in the inputs of the "Data" view, but I am not succeeding. Follow the code of my controller:

public function search(User $request)
{
    $nome = $request->input('teste');
    $users = DB::select('select * from users where nome = ?', $nome);*/
    return view('users.index')->with('id', $users->id);
    return view('users.index')->with('sexo', $users->sexo);
    return view('users.index')->with('idade', $users->idade);  
}

That is, what I am trying to do is that the View "data" receives information from the controller’s SQL query, related to the name that was put in the input of the search page.

1 answer

1


public function search($request)
{
    //pega o valor do campo teste
    $nome = $request->get('teste');
    //busca o primeiro usuario com o nome passado
    $user = DB::select("select * from users where nome = $nome")->first();

    //retorna a view users.index passando as variaveis nome e user
    return view('users.index', compact('nome', 'user'));
}

In your view users/index.php:

{{ $user->id }}
{{ $user->sexo }}
{{ $user->idade }}
{{ $nome }}
  • This "User" is the controller, model or request?

  • would be your model, but updated the question using DB::select to make it easier

Browser other questions tagged

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