Php (Laravel Elount)获取类别和所有嵌套子类别的帖子
我想检索所选类别和所有嵌套子类别的所有帖子。我的意思是这样的:Php (Laravel Elount)获取类别和所有嵌套子类别的帖子,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我想检索所选类别和所有嵌套子类别的所有帖子。我的意思是这样的: [id= 1]-Category 1 [id= 7]--Category 1.1 [id=12]---Category 1.1.1 [id=13]---Category 1.1.2 [id= 9]--Category 1.2 [id= 3]-Category 2 首先,我尝试递归地获取所选类别和所有嵌套子类别的所有ID,然后使用这些类别ID检索帖子 我在controller中编写了方法getSubcategoriesIds($ca
[id= 1]-Category 1
[id= 7]--Category 1.1
[id=12]---Category 1.1.1
[id=13]---Category 1.1.2
[id= 9]--Category 1.2
[id= 3]-Category 2
首先,我尝试递归地获取所选类别和所有嵌套子类别的所有ID,然后使用这些类别ID检索帖子
我在controller中编写了方法getSubcategoriesIds($category)
,它实际上给了我想要的东西,但它返回了带有重复ID的数组。例如,类别1我得到:
array:10 [
0 => 1
1 => 7
2 => 1
3 => 7
4 => 12
5 => 1
6 => 7
7 => 12
8 => 13
9 => 9
]
如何改进返回没有重复ID的方法?
或者也许有更好的方法来恢复这些帖子
$subcategoriesIds = array:10 [
0 => 1
1 => 7
2 => 1
3 => 7
4 => 12
5 => 1
6 => 7
7 => 12
8 => 13
9 => 9
]
这是我的档案:
Model Category.php:
class Category extends Model {
public function parent() {
return $this->belongsTo('App\Category', 'parent_id');
}
public function children() {
return $this->hasMany('App\Category', 'parent_id');
}
public function subcategories() {
return $this->children()->with('subcategories');
}
public function parents() {
return $this->parent()->with('parents');
}
public function posts(){
return $this->hasMany('App\Post');
}
}
public function show($id)
{
$ids =$this->getSubcategoriesIds(Category::with('subcategories')->where('id', $id)->first()));
$posts = Post::whereIn('category_id', $ids)->get();
return view('post.index', compact('posts'));
}
public function getSubcategoriesIds($category) {
$array = [$category->id];
if(count($category->subcategories) == 0) {
return $array;
}
else {
foreach ($category->subcategories as $subcategory) {
array_push($array, $subcategory->id);
if(count($subcategory->subcategories)){
$array = array_merge($array, $this->getChildren($subcategory->subcategories, $array));
}
}
return $array;
}
}
public function getChildren($subcategories, $array) {
foreach ($subcategories as $subcategory) {
array_push($array, $subcategory->id);
if(count($subcategory->subcategories)){
return $array = array_merge($array, $this->getChildren($subcategory->subcategories, $array));
}
return $array;
}
}
PostController.php:
class Category extends Model {
public function parent() {
return $this->belongsTo('App\Category', 'parent_id');
}
public function children() {
return $this->hasMany('App\Category', 'parent_id');
}
public function subcategories() {
return $this->children()->with('subcategories');
}
public function parents() {
return $this->parent()->with('parents');
}
public function posts(){
return $this->hasMany('App\Post');
}
}
public function show($id)
{
$ids =$this->getSubcategoriesIds(Category::with('subcategories')->where('id', $id)->first()));
$posts = Post::whereIn('category_id', $ids)->get();
return view('post.index', compact('posts'));
}
public function getSubcategoriesIds($category) {
$array = [$category->id];
if(count($category->subcategories) == 0) {
return $array;
}
else {
foreach ($category->subcategories as $subcategory) {
array_push($array, $subcategory->id);
if(count($subcategory->subcategories)){
$array = array_merge($array, $this->getChildren($subcategory->subcategories, $array));
}
}
return $array;
}
}
public function getChildren($subcategories, $array) {
foreach ($subcategories as $subcategory) {
array_push($array, $subcategory->id);
if(count($subcategory->subcategories)){
return $array = array_merge($array, $this->getChildren($subcategory->subcategories, $array));
}
return $array;
}
}
您的问题:如何改进返回ID而不重复的方法?或者也许有更好的方法来恢复这些帖子
$subcategoriesIds = array:10 [
0 => 1
1 => 7
2 => 1
3 => 7
4 => 12
5 => 1
6 => 7
7 => 12
8 => 13
9 => 9
]
我的答案是:collect($subcategoriesIds)->unique()->all()
查看Laravel Collection Unique我在递归方法中发现了问题。我不应该将
$array
传递给递归方法getChildrenIds()
以下是我的解决方案(通过一些重构):
现在$this->getCategoriesID(Category::with('subcategories')->where('id',1)->first()代码>返回:
array:10 [
0 => 1
1 => 7
2 => 12
3 => 13
4 => 9
]
我知道我可以从数组中获得唯一的项,但我认为我在递归方法中做了一些错误,并将重复项放到数组中,但我不知道我的方法有什么问题。