1

I am using form with post method and there are multiple checkbox, when i check a checkbox and submit form, it work properly and result come with pagination, but when i click next link of pagination it shows an error of 404 page not found.

View : -

<form action="{{ route('trainer.filter') }}" method="post">
        @csrf
          <!-- Name -->
          <input type="text" class="form-control mb-4" placeholder="Search" name="keywords" aria-label="Search">
          <h6 class="black-text mt-2 "><b class="category">Areas of Experties</b></h6>
          <!-- Copy -->
          @foreach ($expertise_areas as $expertise_area)
            <div class="custom-control custom-checkbox mb-1" style="padding-left: 3px;">
              <label for="expertise_area">
                <input type="checkbox" name="expertise_area[]" value="{{ $expertise_area->id }}">&nbsp;&nbsp;{{ $expertise_area->name }}
              </label>
            </div>
          @endforeach

          <!-- State -->
          <h6 class="black-text mt-2 "><b class="category">Trainer Location (STATE)</b></h6>
          <select class="form-default browser-default custom-select mb-4" name="state" id="state">
             <option selected disabled>Choose State</option>
             @foreach ($states as $state)
             <option value="{{ $state->id }}">{{ $state->name }}</option>
             @endforeach
          </select>
          <h6 class="black-text mt-2 "><b class="category">Trainer Location (City)</b></h6>
          <!-- City -->
          <select class="browser-default custom-select mb-4" name="city" id="city">
             <option selected disabled>Choose City</option>
             @foreach ($cities as $city)
             <option value="{{ $city->id }}">{{ $city->name }}</option>
             @endforeach
          </select>
          <!-- Send button -->
          <button class="btn btn-info btn-block" type="submit">Search</button>
       </form>

Pagination link : -

{{ $trainers->links() }}

Route : -

Route::post('trainer/filter', 'HomeController@trainerFilter')->name('trainer.filter');

Controller : -

public function trainerFilter(Request $request)
{
    if ($request->has('keywords')) {
        $trainers = Trainer::where('name', 'LIKE', '%'.$request->keywords.'%')
                    ->orWhere('state_name', 'LIKE', '%'.$request->keywords.'%')
                    ->orWhere('city_name', 'LIKE', '%'.$request->keywords.'%')->orderBy('ranking', 'asc')->paginate(5);
    }}
1
  • The controller code does not seem a minimal example, please can you add some more details. Commented Feb 28, 2019 at 17:24

1 Answer 1

2

I think you missed to persist the parameters you use in the where inside the controller through the different pages of the resultset.

Looking at the controller code you posted you can try to append the keywords parameter to the pagination links, something like that:

{{ $trainers->appends(['keywords' => $keywords])->links() }}

And remember to pass ['keywords' => $request->keywords] to the view.

Sign up to request clarification or add additional context in comments.

4 Comments

i am not sending keywords, i am only sending expertise_area named checkbox when i check a checkbox, there are multiple search like input type text and checkbox and select box
Watching at your controller code you are using only the keywords parameter to filter $trainers, My answer is based on the code you posted and I think I gave you more than an hint to solve your pagination problem.
ok bro, i got it. actually i am sending more search items. like text box named keywords, checkbox named expertise_area and select box named state and city.
@krish-bhardwaj Just appends more parameters like I show in the answer. Please upvote and accept the answer if it helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.