gpt4 book ai didi

php - 当我在 laravel 和 mongodb 中单击分页时,过滤器不起作用

转载 作者:行者123 更新时间:2023-12-05 06:34:05 25 4
gpt4 key购买 nike

我有一个带分页的过滤器功能。过滤器最初工作。当我单击分页下一个按钮时,结果未根据过滤器显示。

例如:我在过滤框中有三个国家。我选择了两个,然后单击过滤器按钮,然后结果在第一页上显示正确,但是当我单击分页下一个按钮时,所有三个国家/地区的结果都显示未附加过滤器。

感谢您的帮助

框架:laravel 5.5

数据库:MongoDB

route.php

Route::match(['get', 'post'], '/search', 'SearchController@index')->name('search');

SearchController.php

public function index(SearchRequest $request)
{
$data = $request->all();
$cabin = Cabin::select('_id', 'name', 'country', 'region', 'interior', 'sleeping_place', 'height', 'other_details', 'interior')
->where('is_delete', 0)
->where('other_cabin', "0");

if(isset($request->cabinname)){
$cabin->where('name', $request->cabinname);
}

if(isset($request->country)){
$cabin->whereIn('country', $data['country']);
}

//dd($cabin->count());
if(isset($request->region)){
$cabin->whereIn('region', $data['region']);
}

$cabinSearchResult = $cabin->simplePaginate(5);

return view('searchResult', ['cabinSearchResult' => $cabinSearchResult]);
}

search.blade.php

<form action="{{ route('search') }}" method="POST" class="navbar-form navbar-left" id="search-nav-home">

{{ csrf_field() }}

<div class="form-group navbar-form navbar-left" id="prefetch">
<input type="text" class="form-control-home typeahead" name="cabinname" id="cabinname" placeholder="Search Cabin">
</div>

<ul class="nav navbar-nav" id="filter-home">

@if($services->country())
<li class="dropdown">
<!-- Dropdown in Filter -->
<a href="#" class="dropdown-toggle dropdown-toggle-home" data-toggle="dropdown">Country <span class="caret"></span></a>
<ul class="dropdown-menu dropdown-menu-home">
@foreach($services->country() as $land)
<li class="check-it-list-home"><input type="checkbox" class="check-it-home" name="country[]" value="{{ $land->name }}"> {{ $land->name }}</li>
@endforeach
</ul>
</li>
@endif

@if($services->regions())
<li class="dropdown">

<a href="#" class="dropdown-toggle dropdown-toggle-home" data-toggle="dropdown">Region<span class="caret"></span></a>
<ul class="dropdown-menu dropdown-menu-home drop-height">
@foreach($services->regions() as $region)
<li class="check-it-list-home"><input type="checkbox" name="region[]" value="{{ $region->name }}" class="check-it-home"> {{ $region->name }}

</li>
@endforeach
</ul>
</li>
@endif

</ul>

<div class="form-group navbar-form navbar-right" id="navbar-right-filter-home">
<button type="submit" class="btn btn-default-home btn-filter-home">Filter Cabins</button>
</div>
</form>

enter image description here

最佳答案

在结果 View 中,您可能需要将查询链接附加到生成的分页链接:

首先在您的 Controller 中,将搜索结果发送到 View ,例如:

....
return view('searchResult', ['cabinSearchResult' => $cabinSearchResult, 'next_query' => $data]);

然后在您的 View 中,在 $searchResult 上使用 appendslink() 方法,您可以在其中显示分页链接,而不仅仅是 link()render(),像这样:

....
{{ $searchResult->appends($next_query)->links() }}

这应该会生成带有查询字符串的链接,该查询字符串包含所有生成页面上的旧搜索查询。

阅读更多关于分页的信息 Laravel Docs

关于php - 当我在 laravel 和 mongodb 中单击分页时,过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50438135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com