Problems with paging in the Standard

Asked

Viewed 1,148 times

2

I have a page where is listed the title of all news site, and has pagination.

I was asked to put a field of research, to facilitate the search for a certain news.

I was able to make this form and implement the controller, but the problem is this: I type something to filter. It appears right, but when going to another page (by paging), it loses this filter.

Just like I did:

The Controller

public function noticias()
{
    $texto=Input::get('texto');
    $noticias_geral = Noticia::where('ativa','s')
                        ->whereHas('unidades', function($q){
                                    $q->where('not_noticia_unidade.id_unidade', 1);
                                  })
                        ->Where('texto','like','%'.$texto.'%')
                        ->orderBy('id_noticias','DESC')
                        ->paginate(15);

    return view('pages_home.noticias')->with('noticias_geral',$noticias_geral);
}

The route:

Route::any('/noticias', 'NoticiasController@noticias');

the HTML:

@foreach ($noticias_geral as $key=> $dados)
   <div class="col-md-12">
        <div class="linhas_noticia">
             {{ $dados->data->format('d/m/Y') }} - <a href="{!! url('noticia/'.$dados->id_noticias)!!}">{{ $dados->titulo }}</a>
         </div>   
   </div> 
@endforeach
.....................
<div class="col-lg-12">
   {!! $noticias_geral->links() !!}
</div>

What I did wrong?

  • The field texto filter is sent by GET or POST?

1 answer

1


Make the following changes:

No Controller

The amount is redeemed $texto, so you need to give a appends in pagination to have the effect on other pages, send the amount to your View, example:

public function noticias()
{
    $texto = Input::get('texto');
    $noticias_geral = Noticia::where('ativa','s')
                        ->whereHas('unidades', function($q){
                            $q->where('not_noticia_unidade.id_unidade', 1);
                        })
                        ->where('texto','like','%'.$texto.'%')
                        ->orderBy('id_noticias','DESC')
                        ->paginate(15);

    return view('pages_home.noticias')
                  ->with('noticias_geral',$noticias_geral)
                  ->with('texto', $texto);
}

Na View

In his View before printing the links(), put a method appends that is receives a array of variables contained in that pagination, example:

@foreach ($noticias_geral as $key=> $dados)
   <div class="col-md-12">
        <div class="linhas_noticia">
            {{ $dados->data->format('d/m/Y') }} 
             - 
            <a href="{!! url('noticia/'.$dados->id_noticias)!!}">
                {{ $dados->titulo }}
            </a>
         </div>   
   </div> 
@endforeach
.....................
<div class="col-lg-12">
   {!! $noticias_geral->appends(['texto' => $texto])->links() !!}
</div>

and with it the paging will contain the filters sent to all pages.

References

  • That’s right @Virgilionovic!! Thanks!!

  • @Giselepassoni if you can accept the answer to the question

Browser other questions tagged

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