将范围链接到分页方法
Chaining a scope to pagination method
我试图将一个范围链接到分页方法,但我却得到了该方法的 badmethodcallexception;
$products = Product::paginate(3)->forstore();
forstore 方法是范围。
public function scopeForstore($query) {
...
}
我怎样才能达到预期的结果,即向分页查询添加范围?
您应该在调用分页函数之前直接在构建器上应用范围,如下所示:
Product::forstore()->paginate(3);
->paginate(x)
、->get()
、->first()
...方法结束查询。他们告诉查询是时候 return 结果了(他们 return 集合或模型)。您正在做的是在集合上调用范围而不是查询。
交换它们:
$products = Product::forstore()->paginate(3);
我试图将一个范围链接到分页方法,但我却得到了该方法的 badmethodcallexception;
$products = Product::paginate(3)->forstore();
forstore 方法是范围。
public function scopeForstore($query) {
...
}
我怎样才能达到预期的结果,即向分页查询添加范围?
您应该在调用分页函数之前直接在构建器上应用范围,如下所示:
Product::forstore()->paginate(3);
->paginate(x)
、->get()
、->first()
...方法结束查询。他们告诉查询是时候 return 结果了(他们 return 集合或模型)。您正在做的是在集合上调用范围而不是查询。
交换它们:
$products = Product::forstore()->paginate(3);