Laravel 对关系数据使用分页
Laravel using paginate for relationship data
在我的网络应用程序中,作为帖子的每个内容都属于一个或多个类别,并且类别有很多帖子,现在当我使用此代码从中获取数据时:
$categoryContents=ContentCategories::with('contents')->whereId($id)->latest()->paginate(10);
那是 return 这个输出:
LengthAwarePaginator {#971 ▼
#total: 1
#lastPage: 1
#items: Collection {#963 ▼
#items: array:1 [▼
0 => ContentCategories {#862 ▼
#table: "contents_categories"
...
#attributes: array:6 [▶]
#original: array:6 [▶]
...
#relations: array:1 [▼
"contents" => Collection {#962 ▼
#items: array:2 [▼
0 => Contents {#952 ▶}
1 => Contents {#953 ▶}
]
}
]
...
}
]
}
#perPage: 10
#currentPage: 1
#path: "http://127.0.0.1:8000/category/5"
#query: []
#fragment: null
#pageName: "page"
}
在这个分页器中,我试图通过以下代码在视图中显示 contents
数组:
@foreach($categoryContents->contents_categories->contents as $content)
@endforeach
但是我得到这个错误:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$contents_categories
如何在分页器上显示此结构?
我的模特:
class ContentCategories extends Model
{
...
public function contents()
{
return $this->belongsToMany(Contents::class);
}
}
class Contents extends Model
{
...
public function categories()
{
return $this->belongsToMany(ContentCategories::class);
}
}
您可以通过以下方式解决此问题:
$categoryContents = ContentCategories::with('contents')->whereId($id)->latest()->paginate(10);
$categoryContents->getCollection()->transform(function ($item) {
return $item->contents;
});
这个查询:
$categoryContents=ContentCategories::with('contents')->whereId($id)->latest()->paginate(10);
没有多大意义。您正在按 id 查询 ContentCategory
,这将完全匹配 1 个结果,但您仍在使用 latest()
,它对结果进行排序(1 行上没有任何内容)并调用 paginate(10)
,这将对结果进行分页(没有任何内容可以在 1 行上分页)。
您想要分页 contents
,而不是父 ContentCategories
:
// whereHas to scope it only to the category with id = `$id`
$contents = Contents::whereHas('categories', function ($subQuery) use ($id) {
$subQuery->where('id', $id);
})
// Order the cotnents
->latest()
// Paginate the contents
->paginate(10);
然后将 $contents
传递到您的视图并 foreach
覆盖它:
@foreach($contents as $content)
@endforeach
在我的网络应用程序中,作为帖子的每个内容都属于一个或多个类别,并且类别有很多帖子,现在当我使用此代码从中获取数据时:
$categoryContents=ContentCategories::with('contents')->whereId($id)->latest()->paginate(10);
那是 return 这个输出:
LengthAwarePaginator {#971 ▼
#total: 1
#lastPage: 1
#items: Collection {#963 ▼
#items: array:1 [▼
0 => ContentCategories {#862 ▼
#table: "contents_categories"
...
#attributes: array:6 [▶]
#original: array:6 [▶]
...
#relations: array:1 [▼
"contents" => Collection {#962 ▼
#items: array:2 [▼
0 => Contents {#952 ▶}
1 => Contents {#953 ▶}
]
}
]
...
}
]
}
#perPage: 10
#currentPage: 1
#path: "http://127.0.0.1:8000/category/5"
#query: []
#fragment: null
#pageName: "page"
}
在这个分页器中,我试图通过以下代码在视图中显示 contents
数组:
@foreach($categoryContents->contents_categories->contents as $content)
@endforeach
但是我得到这个错误:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$contents_categories
如何在分页器上显示此结构?
我的模特:
class ContentCategories extends Model
{
...
public function contents()
{
return $this->belongsToMany(Contents::class);
}
}
class Contents extends Model
{
...
public function categories()
{
return $this->belongsToMany(ContentCategories::class);
}
}
您可以通过以下方式解决此问题:
$categoryContents = ContentCategories::with('contents')->whereId($id)->latest()->paginate(10);
$categoryContents->getCollection()->transform(function ($item) {
return $item->contents;
});
这个查询:
$categoryContents=ContentCategories::with('contents')->whereId($id)->latest()->paginate(10);
没有多大意义。您正在按 id 查询 ContentCategory
,这将完全匹配 1 个结果,但您仍在使用 latest()
,它对结果进行排序(1 行上没有任何内容)并调用 paginate(10)
,这将对结果进行分页(没有任何内容可以在 1 行上分页)。
您想要分页 contents
,而不是父 ContentCategories
:
// whereHas to scope it only to the category with id = `$id`
$contents = Contents::whereHas('categories', function ($subQuery) use ($id) {
$subQuery->where('id', $id);
})
// Order the cotnents
->latest()
// Paginate the contents
->paginate(10);
然后将 $contents
传递到您的视图并 foreach
覆盖它:
@foreach($contents as $content)
@endforeach