[英]Laravel/Eloquent Paginate returning 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). 我正在为客户创建一个搜索表单,该表单允许多个选项,包括客户的ID(主键)。
$res = new \\App\\Customer;
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. 但是,过滤主键,然后调用
paginate()
返回一个空集合。
>>> $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()
. simplePaginate()
工作正常,但是我想使用完整的paginate()
的其他功能。
(Yes, I understand I will only get a single record when filtering on the primary key however without calling paginate()
on the set my view would break as it uses the pagination methods such as firstItem()
, lastItem()
, total()
, etc.) (是的,我知道在对主键进行过滤时,我只会得到一条记录,但是如果不调用set上的
paginate()
,我的视图就会崩溃,因为它使用了诸如firstItem()
, lastItem()
, total()
类的分页方法等)
尝试:
$res = \\App\\Customer::where('id', 3)->paginate(15);
我不确定它为什么能工作,但是将我的位置移到Model的范围方法中解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.