Laravel 5 - 分页Eloquent系列

时间:2015-04-01 14:43:03

标签: php laravel pagination eloquent laravel-5

型号:

class Category extends Model
{
    public function trainings()
    {
        return $this->hasMany('App\Training');
    }
}

控制器:

return view('category', [
    'trainings' => Category::find(1)->trainings->paginate(10)
]);

我收到Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()错误。

如何在Laravel 5中对Eloquent Collection进行分页?

1 个答案:

答案 0 :(得分:16)

您必须将trainings称为方法:

Category::find(1)->trainings()->paginate(10)
//                          ^^