I want to implement a search form in Laravel, I was watched every video on YouTube to do this, and tried many tutorials but my code doesn’t work.
File web.php
In my routes are a resource with CRUD of clients, which will also have a search, which I want to implement filters, by name, by state, etc…
Route::resource('clients', 'ClientController'); Route::any('/search', 'ClientController@search')->name('search');
File ClientController.php
The function search is a tried to implement the search, but I know is totally wrong, but I don’t find on Google any clear tutorial I understood how to do correctly…
public function search(Request $request) { $nome = $request->nome; $uf = $request->uf; $pesquisa = Client::where('nome', 'like', '%'.$nome.'%') ->orderBy('nome') ->paginate(5); return view('clients.index') ->with('pesquisa', $pesquisa) ->with('clients', $clients); }
File index.blade.php
This is my page on Front-end… Has a table of all clients, and on top I want to implement a search with filters by name and by state…
@section('title', 'Clientes') @section('content_header') <h1> Clientes <a href="{{ route('clients.create') }}" class="btn btn-sm btn-success"> Novo Cliente </a> </h1> @endsection @section('content') <div class="box"> <div class="box-header"> <nobr> <form action="/painel/search" method="POST" class="form form-inline" role="search"> @csrf <div class="col-2"> <input type="text" name="nome" class="form-control" placeholder="Nome"> <input type="text" name="uf" class="form-control" placeholder="UF"> <button type="submit" class="btn btn-primary">Pesquisar</button> </div> </form> </nobr> </div> </div> </br> <div class="card"> <div class="card-body"> <table class="table table-hover"> <thead> <tr> <th>Nome</th> <th>UF</th> <th>Telefone</th> <th> <nobr>Ações</nobr> </th> </tr> </thead> <tbody> @foreach($clients as $client) <tr> <td>{{$client->nome}}</td> <td>{{$client->uf}}</td> <td>{{$client->telefone}}</td> <td> <nobr> <a href="{{ route('clients.edit', ['client' => $client->id]) }}" class="btn btn-sm btn-info">Editar</a> <form class="d-inline" method="POST" action="{{ route('clients.destroy', ['client' => $client->id]) }}" onsubmit="return confirm('Tem certeza que deseja excluir o cliente?')"> @method('DELETE') @csrf <button class="btn btn-sm btn-danger">Excluir</button> </form> </nobr> </td> </tr> @endforeach </tbody> </table> </div> </div> {{ $clients->links() }} @endsection
Advertisement
Answer
You don’t have the $clients
variable defined in your controller, so it would not make sense to include it in the return
. Also, seeing that in your view, you loop over that variable, I think that if you simply change the name of the variable where you save the result of your query from $pesquisa
to $clients
, everything will work:
public function search(Request $request) { $nome = $request->nome; $clients = Client::where('nome', 'like', '%'.$nome.'%') ->orderBy('nome') ->paginate(5); return view('clients.index') ->with('clients', $clients); }
Also, your form action it’s maybe wrong, try using the route name instead the uri:
<form action="{{ route('search') }}" //...