Answer the question
In order to leave comments, you need to log in
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>
@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>
$('#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::get('/cards/search', '[email protected]')->name('search');
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]);
}
}
Answer the question
In order to leave comments, you need to log in
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());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question