R
R
Ruslan Gillyazanov2018-10-21 22:01:38
Laravel
Ruslan Gillyazanov, 2018-10-21 22:01:38

How to implement pagination for the search result?

There is a form

<form method="get" action="/cards/search" id="formSearch">
    <div class="form-group">
        <label for="classHeroes">Класс</label>
        <select class="form-control" id="classHeroes" name="playerClass">
            <option>Все</option>
            @foreach ($heroes as $hero)
                <option value="{{ $hero->id }}">{{ $hero->name }}</option>
            @endforeach
        </select>
    </div>
    <button type="submit" class="btn btn-search-color btn-block">Найти</button>
</form>

The result of the query is displayed in the template
@foreach ($cards as $card)
    <div class="block-card d-flex justify-content-center" data-toggle="tooltip-cards" data-placement="right">
        {{ card->name }}
    </div>
@endforeach
<div class="container">
    <div class="row d-flex justify-content-center">
        {{ $cards->links() }}
    </div>
</div>

Handles an Ajax form
$('#formSearch').submit(function (event) {
    event.preventDefault();
    var query = $(this).serialize();

    $.ajax({
        type: 'GET',
        url: '/cards/search',
        data: query,
        success: function (data) {
            $('.block-cards').html(data);
        }
    });
});

/ * Пагинация Ajax результата поиска */

$(function() {
    $('body').on('click', '.pagination a', function(e) {
        e.preventDefault();

        var url = $(this).attr('href');
        getCards(url);
    });

    function getCards(url) {
        $.ajax({
            type: 'GET',
            url : url
        }).done(function (data) {
            $('.block-cards').html(data);
        });
    }
});

route
Route::get('/cards/search', '[email protected]')->name('search');

Controller
public function search(Request $request)
{
    if ($request->ajax())
    {
        $query = $request->all();
            
        // Удаляем данные из запроса со значением "Все"
        foreach ($query as $key => $item) {
            if ($query[$key] == 'Все')
                unset($query[$key]);
        }

        $this->cards = Card::query()->where($query, '=', array_flip($query))->paginate($this->perPage)
                                                   ->appends($query);

        return view('default.cards.cardsload', ['cards' => $this->cards]);
    }       
}

The problem is this. When I fill out the form and execute the request, the data is displayed, but if I select another page in the pagination, then nothing happens.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Gillyazanov, 2018-10-22
@trycode

Solved, it was necessary to remove the $page parameter from the request, since it was substituted in the select request

// Удаляем данные из запроса со значением "Все"
        foreach ($query as $key => $item) {
            if ($key == 'page' || $query[$key] == 'Все')
                unset($query[$key]);
        }

        $this->cards = Card::query()->where($query, '=', array_flip($query))->paginate($this->perPage)->appends($request->all());

D
Dmitry, 2018-10-22
@swede2k

Most likely, when paginating, you do not pass search terms. The pagination link should look something like this - /search?page=2&query=Superman

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question