php - Filter is not working when I click on pagination in laravel and mongo db

507

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', '[email protected]')->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" id="search-nav-home">

  {{ csrf_field() }}

  <div id="prefetch">
     <input type="text" name="cabinname" id="cabinname" placeholder="Search Cabin">
  </div>

   <ul id="filter-home">

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

     @if($services->regions())
       <li >

       <a href="#" data-toggle="dropdown">Region<span ></span></a>
         <ul >
          @foreach($services->regions() as $region)
          <li ><input type="checkbox" name="region[]" value="{{ $region->name }}" > {{ $region->name }}

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

     </ul>

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

enter image description here

People are also looking for solutions to the problem: php - Split array of strings on first space and make new array grouping by first word

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.