Laravel 4 Eloquent paginate相关模型

时间:2014-04-11 21:08:03

标签: php laravel pagination relational-database eloquent

我正在使用有两个模型的Laravel项目:Offer belongsTo Category

当我检索所有记录时,很容易对结果进行分页:

$offers = Offer::paginate(10);

但是,当我尝试仅检索具有特定Offers的{​​{1}}时,它只是不起作用:

Category

我收到此错误:

$category = Category::whereId($category_id)->with('offers')->first()->paginate(10);

UPDATE:

我通过用以下代码替换第二个代码来解决它:

 Undefined property: Illuminate\Pagination\Paginator::$offers 

1 个答案:

答案 0 :(得分:1)

用这个替换第二个代码:

$category = Category::find($category_id);
$offers = $category->offers()->paginate(10);