問(wèn)題描述
我正在嘗試為這樣的 Eloquent 關(guān)系分頁(yè):
I am trying to paginate a Eloquent relationship like this:
$query = Product::find(1)->options()->paginate();
但我收到以下錯(cuò)誤:
Fatal error: Call to a member function getCurrentPage() on a non-object
我已經(jīng)確認(rèn)代碼 $query = Product::find(1)->options()
返回一個(gè)選項(xiàng)集合.$query
對(duì)象似乎是 hasMany
類(lèi)型.以下是我正在使用的模型類(lèi).
I have confirmed that the code $query = Product::find(1)->options()
returns a collection of options. The $query
object seems to be of type hasMany
. Below are the model classes I am using.
class Product extends Eloquent
{
protected $table = 'products';
public function options ()
{
return $this->hasMany('ProductOption', 'product_id');
}
}
class ProductOption extends Eloquent
{
protected $table = 'product_options';
public function product()
{
return $this->belongsTo('Product', 'product_id');
}
}
eloquent 不返回關(guān)系的分頁(yè)結(jié)果嗎?
Does eloquent not return paginated results for relationships?
推薦答案
你不能像這樣延遲加載關(guān)系分頁(yè),而是在你的產(chǎn)品模型中把以下函數(shù)放在你的選項(xiàng)下面有很多關(guān)系
You can not lazy load relational pagination like that, instead in your Product Model put the following function below your options has many relationship
public function getOptionsPaginatedAttribute()
{
return $this->options()->paginate(10);
}
這將允許您通過(guò)
$product->options_paginated
這篇關(guān)于Laravel Eloquent 關(guān)于關(guān)系的分頁(yè)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!