douao2000 2018-05-20 18:29
浏览 218
已采纳

当我点击laravel和mongodb中的分页时,过滤器无法正常工作

I have a filter functionality with pagination. Filter is working initially. When I click on pagination next button result is not showing based on filter.

eg: I have three countries in filter box. I chosen two then click on filter button then result is showing correct on first page but When I click on pagination next button all three countries result is showing not appending filter.

Thank you any help would be appreciated

Framework: laravel 5.5

Database: 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

  • 写回答

1条回答 默认 最新

  • drrvnbwle80177811 2018-05-21 11:12
    关注

    In the result View you may need to append the query link to the generated paginate link:

    First in your controller, send the result of the search to the view e.g.:

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

    Then in your View, use appends and link() method on $searchResult where you display the paginated link instead of only link() or render(), like this:

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

    This should generate links with the query string that contains your old search query on all the pages generated.

    Read more about pagination in Laravel Docs

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。