Laravel 4 如何调用Laravel访问器

Laravel 4 如何调用Laravel访问器,laravel-4,accessor,Laravel 4,Accessor,在产品模型中 public function getPaginatedAttribute() { $posts = $this->posts; return \Paginator::make($posts, $posts->count(), 10); } 当呼叫控制器时 $product = Product::with('categories')->paginated(); 总是出错 Call to undefined method Illuminate\

在产品模型中

public function getPaginatedAttribute()
{
    $posts = $this->posts;

    return \Paginator::make($posts, $posts->count(), 10);
}
当呼叫控制器时

$product = Product::with('categories')->paginated();
总是出错

Call to undefined method Illuminate\Database\Query\Builder::paginated(); 

我想你不需要访问器

您可以在模型中创建函数,然后从控制器调用它

产品型号:

public static function paginated()
{
    $posts = self::with('categories')->get();

    return \Paginator::make($posts, $posts->count(), 10);
}
控制器:

$product = Product::paginated();
访问器用于转换模型的属性:


我不确定这是否是您在本例中想要的。

在本例中,您会收到该错误,因为您试图从生成器调用访问器

$product = Product::with('categories')->get()->paginated();