Problems with paging in laravel

2

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

I was asked to put a search field, to make it easier to search for a particular piece of news.

I was able to do this form and the controller implementation, but the problem is this: I type something to filter. It appears right, but when you go to another page (by pagination), you lose this filter.

Here's how I did it:

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');

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 did I do wrong?

    
asked by anonymous 23.11.2017 / 19:39

1 answer

1

Make the following changes:

No Controller

The $texto value is redeemed, so you need to give a appends in example in> :

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

On your View before printing View , put a appends method. which is given a links() of variables contained in this page, 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 this the paging will contain the filters sent to all pages.

23.11.2017 / 21:05