K
K
Klayn242015-05-07 20:45:01
Laravel
Klayn24, 2015-05-07 20:45:01

Laravel 4 pagination when searching?

Good day. The bottom line is this - I make a get request to the controller, which finds the products I need with a like (LIKE) and displays them with pagination, but when I go to page 2, 3, etc. , then the ?search parameter becomes empty (was /search?search="some query" , became /search?page=3 ). How to implement the url /search?search="request"&page=3 , or /search/request/3 , if it is easier to implement? Otherwise, on the first page, the products taken from the search are displayed, and on the next - just all the products ... If I don't explain myself clearly, I'm sorry :)
Rout

Route::get('store/search', array('uses'=>'[email protected]'));

Controller:
public function getSearch()
    {
        $search = Input::get('search');


        return View::make('store.index')
            ->with('products', Product::where('title', 'LIKE', '%'.$search.'%')->paginate(3))
            ->with('search', $search);
    }

Search Form in Layout
<div class="search_box pull-right">
         {{ Form::open(array('url'=>'store/search', 'method'=>'get')) }}
         {{ Form::text('search', null, array('placeholder'=>'Поиск')) }}
         {{ Form::submit('Поиск', array('class'=>'')) }}
         {{ Form::close() }}
</div>

In the view, the navigation output is:
@section('pagination')
    <div id="pagin">
        {{ $products->links() }}
    </div>
    @stop

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rikcon, 2015-05-07
@Klayn24

appends(string $key, string $value = null)
laravel.com/api/4.2/Illuminate/Pagination/Paginato...
Here's the method you need, try it, if it doesn't fit, there are a couple more interesting methods, like

addQuery(string $key, string $value)

If it is not clear then use it like this:
->with('products', Product::where('title', 'LIKE', '%'.$search.'%')->paginate(3)->appends( 'key','value'));

M
Mokhirjon Naimov, 2015-05-08
@zvermafia

{{ $products->appends(array('search' => $search))->links() }}

Details here: PARAMETERS IN LINKS .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question