Shobo's avatar
Level 1

Paginate returns empty collection when filtering on primary key

I'm working on creating a search form for customers that allows multiple options, including the customer's id (primary key).

When filtering on a column that isn't the primary key, everything works fine:

>>> $res = \App\Customer::where('customer_name', 'like', "%frank%")->first();
=> App\Customer {#736}
>>> $res = \App\Customer::where('customer_name', 'like', "%frank%")->paginate(15)->first();
=> App\Customer {#749}

However, filtering on the primary key and then calling paginate() returns an empty collection.

>>> $res = \App\Customer::where('id', 3)->first();
=> App\Customer {#669}
>>> $res = \App\Customer::where('id', 3)->paginate(15)->first();
=> null

simplePaginate() works fine, however I'd like to use the additional features from the full paginate().

Is there any way around this? Thanks.

(Yes, I understand I will only get a single record when filtering on the primary key however without calling paginate on the set it would break my view as it uses the pagination methods such as firstItem(), lastItem(), total(), etc.)

Edit:

I've created a new project with nothing but the empty model and get the exact same output. Tested on both v5.3.2 and v5.3.4

0 likes
1 reply
Shobo's avatar
Level 1

Not sure why it worked, but moving the where into a scope solved the problem.

Please or to participate in this conversation.