Redirect error when paging in laravel

0

Good morning, I am currently doing a project in laravel 5.2

I have been developing a simple search engine, but I have had one or another problem with paging, always when I make a query through the search engine and decide to go to the next page of records redirect me to the index of the project.

<div class="search_box">
    {!!Form::open(['route'=>'buscar.store', 'method'=>'POST'])!!}
    <div class="input-group">
        <input  name="descripcion" type="text" placeholder="Buscar en {{ strtolower($categoria->subcategoria) }} ">
        <input  type="hidden" name="subcategoria" id="subcategoria" value="{{ $categoria->subcategoria }}">
        <span class="input-group-btn">
            <button class="btn btn-default buscar" type="submit">Buscar</button>
        </span>
    </div>
    {!!Form::close()!!}
</div>

I leave the driver

 public function store(request $request)
{

    $descripcion = $request->input("descripcion");
    $subcategoria = $request->input("subcategoria");
    $categoria = $request->input("categoria");

    if (is_null($categoria)) {

    $producto = self::join('ped_unidades_medida','vcomp_productos_web1.unidad','=','ped_unidades_medida.unidad_uid')
        ->where('vcomp_productos_web1.marca','forte')
        ->where('vcomp_productos_web1.venta_web','SI')
        ->where('vcomp_productos_web1.subcategoria',$subcategoria)
        ->where('vcomp_productos_web1.descripcion','like','%'.$descripcion .'%')
        ->select(['vcomp_productos_web1.*','ped_unidades_medida.unidad_descripcion'])
        ->paginate(9);

        return view('productos.buscar', ['articulos' => $respuesta, 'categorias' => $categorias, 'destacados' => $destacados, 'categoria'=> $categoria]);

    } else {


    $producto = self::join('ped_unidades_medida','vcomp_productos_web1.unidad','=','ped_unidades_medida.unidad_uid')
        ->where('vcomp_productos_web1.marca','forte')
        ->where('vcomp_productos_web1.venta_web','SI')
        ->where('vcomp_productos_web1.categoria',$categoria)
        ->where('vcomp_productos_web1.descripcion','like','%'.$descripcion .'%')
        ->select(['vcomp_productos_web1.*','ped_unidades_medida.unidad_descripcion'])
        ->paginate(3);

        return view('productos.buscar', ['articulos' => $respuesta, 'categorias' => $categorias, 'destacados' => $destacados, 'categoria'=> $categoria]);

    }

}

And in the products view, I'm looking for the next foreach with paging.

@foreach ($articulos as $articulo)

<div class="col-sm-4">
    <div class="product-image-wrapper">
        <div class="single-subcategory">
            <div class="productinfo text-center ">
                <a href="{{ route('detalle',[$articulo->id]) }}#detalle"><img src="https://www.dyna.com.co/thumb/phpThumb.php?src=http://190.248.128.69:8002/public/imagenescatalogo/{{ $articulo->codigo }}.jpg&w=350&h=350&far=1&bg=ffffff&q=99"  width="150" height="150" alt="" /></a>
                <h2>{{ $articulo->descripcion }}</h2>
                <p><b>Código: </b>{{ $articulo->codigo }} | <b>Empaque: </b>{{ $articulo->unidad_descripcion }}</p>
                <a href="{{ route('detalle',[$articulo->id]) }}#detalle"><button type="button" class="btn btn-default add-to-cart"><i class="fa fa-search"></i>Ver detalle</button></a>
            </div>
        </div>
    </div>
</div>

@endforeach

    {{ $articulos->render() }} 
    
asked by RubxnMC 20.11.2017 в 21:26
source

1 answer

1

The first thing you have to keep in mind is that paging is always done with GET for the simple reason that you can go to the page you want, for example:

http://midominio/search/?text=mi-busqueda&page=2

There you would be on page 2 of the search mi búsqueda . In this way it is easy to index it and to pass the parameters to Laravel . Technically you could do it for POST , but it's more complicated since the URL is always the same.

In fact, in your controller you should have the following:

User::paginate(10);

So automatically page Laravel a Model . In your case as it is a query , you should use builder : link

Here is a response from StackOverflow in English that gives you a couple of examples: link

    
answered by 21.11.2017 / 09:51
source