我正在开发 Laravel 项目,该项目有两个模型: Offer 属于 Category.

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

$offers = Offer::paginate(10);

但当我试图找回时 Offers 具有特定的 Category, ,它只是不起作用:

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

我收到此错误:

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

更新:

我通过用以下代码替换第二个代码解决了这个问题:

$category = Category::find($category_id);
$offers = $category->offers()->paginate(10);
有帮助吗?

解决方案

将第二个代码替换为:

$category = Category::find($category_id);
$offers = $category->offers()->paginate(10);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top