【问题标题】:Getting Parent and Child data (Tree Checkbox) with Laravel使用 Laravel 获取父子数据(树复选框)
【发布时间】:2018-06-08 03:07:05
【问题描述】:

我正在尝试使用 Laravel 从数据库中获取所有父子数据。在父级中也会有嵌套的父级和子级。这就像一个多层次的东西。我面临的问题是使用循环获取所有这些信息。下面是我的代码。

$totalchild = self::getParentChildren($parent->id);

public function getParentChildren($parent) {
    $totalChild = array();
    $child = Permission::where('status', 1)->where('parentid', '=', $parent)->pluck('permissionid')->toArray();
    $totalChild = $child;
    foreach($child as $childs){
        $innerChild[] = self::getChildren($childs);
        $totalChild = array_push($totalChild, $innerChild);
    }

    return $totalChild;
}

我得到的错误是“array_push() 期望参数 1 是数组,给定整数”。我不知道那里出了什么问题。基本上我试图在这个父母 ID 下获取所有父母 ID 和孩子 ID。谢谢!

编辑:数据库示例如下。

id    name        parentid
1     master      0
2     parent A    1          
3     parent B    1
4     child A     2
5     child B     2
6     child C     3
7     child A_A   4
8     child A_B   4

假设我想让所有父母和孩子都在这个主父母 id 1 下。

代码更新: 我想出了另一个,但我不知道为什么它只返回第一层。它并没有把它下面的所有父母和孩子都归还给我。

public function getChildren($parent, $tree_string=array()) {
    $tree = array();
    $tree = Permission::where('status', 1)->where('parentid', $parent)->pluck('permissionid')->toArray();
    if(count($tree)>0 && is_array($tree)){
        $tree_string=array_merge($tree_string,$tree);
    }
    foreach ($tree as $key => $value) {
        self::getChildren($value, $tree_string);
    }
    return $tree_string;
}

$totalchild = self::getChildren($parent->id); // returns me the first layer only

【问题讨论】:

  • 请分享数据库结构和一些示例数据。
  • 已添加数据库,谢谢。

标签: php laravel


【解决方案1】:

其实你可以定义父子关系nEloquent模型。

// app/Category.php
class Category extends Model
{
    ...
    // Define Eloquent parent child relationship
    public function parent() {
        return $this->belongsTo(self::class, 'parent_id');
    }

    // for first level child this will works enough
    public function children() {
        return $this->hasMany(self::class, 'parent_id');
    }

    // and here is the trick for nestable child. 
    public static function nestable($categories) {
       foreach ($categories as $category) {
           if (!$category->children->isEmpty()) {
               $category->children = self::nestable($category->children);
            }
        }

        return $categories;
    }
    ...
}

示例用法:

// Get all parent top level category
$categories = Category::where('parent_id', 0);

// Get nestable data
$nestable = Category::nestable($categories);

希望对你有帮助。

【讨论】:

  • laravel 的良好实现
  • 是否可以返回数组中的所有id?
  • 当然你可以用同样的循环来做。但不确定你想要什么样的数组结构。因为如果它只包含id,它将如何嵌套。你能举出你的数组输出例子吗?
  • 我不知道为什么,但你的代码可能对我不起作用,我可能在我的控制器内做所有事情。但我尝试修改但没有帮助..也许帮我看看我的新代码?我非常接近获取所有数据,但它只返回第一层......谢谢!
【解决方案2】:

通过修改函数解决了问题...希望对其他人有用。

public function getChildren($parent) {
    $tree = Array();
    if (!empty($parent)) {
        $tree = Permission::where('status', 1)->where('parentid', $parent)->pluck('permissionid')->toArray();
        foreach ($tree as $key => $val) {
            $ids = self::getChildren($val);
            if(!empty($ids)){
                if(count($ids)>0) $tree = array_merge($tree, $ids);
            }
        }
    }
    return $tree;
}

【讨论】:

    【解决方案3】:
    public function allParent($child) {
            $tree = [];
            
            while($child) {
                array_push($tree, $child);
                $child = $child->parent;
            }
            return $tree;
    }
    

    你可以给你的 $model->parent 作为 $this->allParent($model->parent)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-18
      • 2014-06-22
      • 2020-04-11
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多