FounderStartup's avatar

Pagination in filter page not working properly

I need to filter the brokers list page with dropdown city

My controller :

 public function Brokersincity(Request $request){

        $city = $request->project_city;
        $brokerscount = User::where('role', 2 )->where('city', $city )->where('status', 1)->count();
        $brokers = User::where('role', 2 )->where('city', $city )->where('status', 1)->paginate(2);
        $cities = Cities::where('status', 1)->orderBy('city_name','ASC')->latest()->get();

  	    return view('frontend.broker.searchbrokers',compact('brokerscount','brokers', 'cities' ));

    } // end method

My blade

       <div class="col-xl-3 col-lg-4 col-md-12">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Search Brokers</h3>
                        </div>
                        <form action="{{ route('search.citybrokers')}}" method="get">
                            @csrf
                            <div class="card-body">
                                    <div class="">
                                        <div class="form-group">
                                            <label class="form-label">City</label>
                                            <select name="project_city" id="select-city" required="" class="form-control 						     select-sm w-100 select2">
                                                <option value="0">Select City</option>
                                                @foreach ($cities as $data)
                                                <option value="{{$data->id}}">
                                                    {{$data->city_name}}
                                                </option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                            </div>
                            <div class="card-footer ">
                                <button type="submit" class="btn btn-primary ml-auto">Search Brokers</button>
                            </div>
                        </form>
                    </div>
                </div>

The first result page displays correctly but the second page displayed zero records. ? Also I need to show the selected city in the dropdown control on the result page.

0 likes
9 replies
tykus's avatar
tykus
Best Answer
Level 104

The first result page displays correctly but the second page displayed zero records

If you need the pagination links to have the existing GET query params, then the withQueryString method will append those existing params:

$brokers = User::where('role', 2 )->where('city', $city )->where('status', 1)->paginate(2)->withQueryString();

https://laravel.com/docs/8.x/pagination#appending-query-string-values

The first result page displays correctly but the second page displayed zero records

You need to add the selected attribute on one of the options corresponding to the selected City, e.g.

@foreach ($cities as $data)
    <option value="{{$data->id}}" {{ request('project_city') == $data->id ? 'selected' : '' }}>
        {{$data->city_name}}
    </option>
@endforeach
1 like
FounderStartup's avatar

@tykus Thanks for your precious time.

I changed in the controller but getting this error :

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::withQueryParams does not exist.

tykus's avatar

@FounderStartup what is the result of:

$brokers = User::where('role', 2 )->where('city', $city )->where('status', 1)->paginate(2);
dd($brokers);
FounderStartup's avatar

Illuminate\Pagination\LengthAwarePaginator {#1765 ▼ #total: 4 #lastPage: 2 #items: Illuminate\Database\Eloquent\Collection {#1760 ▶} #perPage: 2 #currentPage: 1 #path: "http://127.0.0.1:8000/SearchCityBrokers" #query: [] #fragment: null #pageName: "page" +onEachSide: 3 #options: array:2 [▶] }

tykus's avatar

@FounderStartup that's okay... I actually gave you the wrong method... it is withQueryString, not withQueryParams

1 like
FounderStartup's avatar

The first result page displays correctly. But when we try to check second page it shows zero records.

Please or to participate in this conversation.