1
I have on my home screen a search filter, when I perform the search is sent by POST the filter data and I can’t get the pagination link redirected with the search filters.
Every time I have filtered search and click on any of the links this filter gets lost.
Controller:
public function index()
{
return $this->listar();
}
public function listar(Request $request = NULL)
{
if ( !empty( $request )) {
$data = $request->all();
} else {
$data = NULL;
}
$validator = NULL;
if ( empty( $data['busca'] ) ) {
$pacientes = Paciente::joinPessoa()->paginate(10);
}
else
{
if ($data['tipo_busca'] == 'nome') {
$pacientes = DB::table('pacientes AS pac')
->leftJoin('pessoas', 'pac.id_pessoa', '=', 'pessoas.id')
->select('pessoas.nome AS nome','pessoas.dtnasc', 'pac.ficha_atendimento', 'pac.id')
->where($data['tipo_busca'], 'ilike', $data['busca'] . '%')
->orderby('nome')
->paginate(10);
} else {
$validate = $this->validate($request, [
'busca' => 'numeric',
]);
$pacientes = DB::table('pacientes AS pac')
->leftJoin('pessoas', 'pac.id_pessoa', '=', 'pessoas.id')
->select('pessoas.nome AS nome', 'pessoas.dtnasc', 'pac.ficha_atendimento', 'pac.id')
->where('pac.'.$data['tipo_busca'], '=', $data['busca'])
->orderby('nome')
->paginate(10);
}
}
$title = "Listagem de pacientes";
$links = $pacientes->links();
return view('paciente.list', compact('title', 'pacientes', 'links'))
->withErrors($validator);
}
View:
<div class="container-fluid form">
@if( isset($errors) && count( $errors ) > 0 )
<div class='alert alert-danger'>
@foreach( $errors->all() as $erro )
<p>{{ $erro }}</p>
@endforeach
</div>
@endif
<div class='row'>
<div class="col-md-10">
<form method='post' action="{{ route('paciente.listar') }}" class='form-horizontal'>
{!! csrf_field() !!}
<div class="col-md-5">
<input type="text" class="form-control" id="busca" name='busca' placeholder="Busca" value="{{ old('busca') }}" />
</div>
<label for="nome" class="col-sm-2 control-label">Buscar por:</label>
<div class="col-sm-3">
<select class='form-control' name='tipo_busca'>
<option value='nome'>Nome</option>
<option value="ficha_atendimento">Ficha Atendimento</option>
<option value="id">Código</option>
</select>
</div>
<div class="col-md-2">
<button type='submit' class='btn btn-primary pull-right'>
<i class='glyphicon glyphicon-search'></i>
Buscar
</button>
</div>
</form>
</div>
<div class="col-md-2">
<a href="{{ route('paciente.create') }}" class='btn btn-default btn-add'>
<i class='glyphicon glyphicon-plus'></i>
Novo
</a>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class='table table-striped'>
<thead>
<tr>
<th>Código</th>
<th>Nome</th>
<th>Data Nascimento</th>
<th>Ficha de Atendimento</th>
<th width='100px'>Ações</th>
</tr>
</thead>
@forelse($pacientes as $paciente)
<tr>
<td>{{ $paciente->id }}</td>
<td>{{ $paciente->nome }}</td>
<td>{{ \Carbon\Carbon::parse( $paciente->dtnasc )->format( 'd/m/Y' ) }}</td>
<td>{{ $paciente->ficha_atendimento }}</td>
<td>
<a href="{{route( "paciente.edit", $paciente->id )}}" class='actions edit'>
<span class='glyphicon glyphicon-pencil'></span>
</a>
<a href='#' class='actions delete'>
<span class='glyphicon glyphicon-trash'></span>
</a>
</td>
</tr>
@empty
<tr>
<td colspan="4">
Nenhum paciente cadastrado
</td>
</tr>
@endforelse
<tfoot>
<tr>
<td colspan="5" class='text-center'>
{{ $links }}
</td>
</tr>
</tfoot>
</table>
</div>
</div>
Amigo already has the solution at this link: https://answall.com/questions/207623/problema-com-busca-e-pagina%C3%A7%C3%A3o-no-Laravel/208273#208273
– novic
and then it didn’t work out...
– novic