Alexander Yefremov
Alexander Yefremov

Reputation: 63

Laravel 5 pagination

I made pagination like in guides.I doing a search in db, and refer variable with data ti view.

public function search(Request $request)
{
    $products = DB::table('product')->where('number', 'like', $request->number.'%')->paginate(5);
    return view('site.content_layouts', [
        'products' => $products
    ]);
}

Here is part of my html file

 @foreach($products as $product)
                <form role="form" method="get" action="{{ route('add_to_cart') }}">
                <tr>
                    <input type="hidden" name = "id" value="{{$product->id}}">

                    <td>{{$product->manufacturer}}</td>
                    <input type="hidden" name = "manufacturer" value="{{$product->manufacturer}}">

                    <td>{{$product->number}}</td>
                    <input type="hidden" name = "number" value="{{$product->number}}">

                    <td>{{$product->name}}</td>
                    <input type="hidden" name = "name" value="{{$product->name}}">

                    <td>{{$product->quantity}}</td>
                    <input type="hidden" name = "quantity" value="{{$product->quantity}}">

                    <td>{{$product->incoming_time}}</td>
                    <input type="hidden" name = "trader" value="{{$product->trader}}">

                    <td>{{$product->price}}</td>
                    <input type="hidden" name = "price" value="{{$product->price}}">

                    <td><input size="5px" type="text"  id="quantity_choose" name="quantity_choose" value="1"></td>

                    <td>
                        <p data-placement="top" data-toggle="tooltip" title="AddToCart">
                            <button class="btn btn-danger btn-xs" data-title="AddToCart" data-toggle="modal" data-target="#AddToCart" >
                                <span class="glyphicon glyphicon-trash">

                                </span>
                                </button>
                        </p>
                    </td>

                </tr>
                </form>
            @endforeach
            </tbody>
        </table>{{$products->links()}}

First page displayed normally. But when I follow the links of other pages, there displayed first 5 lines of ALL my db, and created links to 61k pages.Not only lines what I search like in first page - its deduces all my db. Help me please) Where is a problem?

1)First page, where al is normally 2)Here I use link of second page

Upvotes: 1

Views: 278

Answers (1)

xmhafiz
xmhafiz

Reputation: 3538

You should use appends() if your pagination has extra parameters. It will add more GET parameters to your url so the search queries will be passed to $request in second page . Refer detail here.

Your code should look similarly like below.

$products = DB::table('product')->where('number', 'like', $request->number.'%')->paginate(5)->appends($request->all());

Above code will carry all parameters passed to the $request.

Upvotes: 1

Related Questions