stefanp's avatar

filtered result after pagination

Hi, I have the followinng query builder

$unitBuilder = Unit::isActive()->with('city')->where('has_images', 1)->where('closed', 'no')->where('person_nr', '>=', $guests])->orderBy('rating');

if I try to filter the results with:

$result = $unitBuilder->where('name', 'My Unit Name')->whereIn('city_id', [1, 5, 6])->get();

i have the following dd() output which is ok

Illuminate\Database\Eloquent\Collection {#2686 ▼
  #items: array:1 [▼
    0 => App\Domain\Units\Unit\Unit {#2688 ▶}
  ]
}

but if I try to paginate with:

$result = $unitBuilder->where('name', 'My Unit Name')->whereIn('city_id', [1, 5, 6])->paginate(10);

The dd() output shows me an empty items[]:

lluminate\Pagination\LengthAwarePaginator {#2691 ▼
  #total: 1
  #lastPage: 1
  #items: Illuminate\Database\Eloquent\Collection {#2685 ▼
    #items: []
  }
  #perPage: 5
  #currentPage: 2
  #path: "http://localhost/my-path/some-other-path"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▶]
}

Why's that?

In need my item in the items array within the paginator object.

Thanks in advance!

0 likes
2 replies
click's avatar

Because you are on a route with ?page=2 in the addressbar probably.

Take a look at the result you show here, it says the currentPage = 2, the last page is 1 and the total amount of items 1. If you are on page 2 there are 0 items.

1 like

Please or to participate in this conversation.