Php 我试图使用嵌套循环来获取子类别,但最终出现了一个错误";属性[Category]在此集合实例上不存在;

Php 我试图使用嵌套循环来获取子类别,但最终出现了一个错误";属性[Category]在此集合实例上不存在;,php,laravel-7,Php,Laravel 7,我已经在我的类别模型中定义了自我关系 public function Category() { return $this->hasmany(Category::class); } 在我的迁移中,我定义了 public function up() { Schema::create('categories', function (Blueprint $table) { $table-&

我已经在我的类别模型中定义了自我关系

 public function Category()
        {
            return $this->hasmany(Category::class);
        }
在我的迁移中,我定义了

 public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->String('Parent_id');
            $table->String('name');
            $table->String('status')->nullable();
            $table->timestamps();
        });
    }

在我的控制器中,我试图获得category的值

 return view('index')->with('cate',Category::all());
我正在尝试应用嵌套循环以通过我的刀片文件中的子类别

 <div class="list-group list-group-collapse list-group-sm list-group-tree" id="list-group-men" data-children=".sub-men">
                           @foreach($cate as $cat) 
                            <div class="list-group-collapse sub-men">
                            <a class="list-group-item list-group-item-action" href="{{$cat->id}}" data-toggle="collapse" aria-expanded="true" aria-controls="{{$cat->id}}">{{$cat->name}}<small class="text-muted">(100)</small>
                            </a>
                            
                        <div class="collapse show" id="{{$cat->id}}" data-parent="#list-group-men">
                                    <div class="list-group">
                                      @foreach($cate->Category as $sub)
                                    <a href="#" class="list-group-item list-group-item-action active">{{$sub->name}} <small class="text-muted">(50)</small></a>
                                      @endforeach  
                                    </div>
                                </div>
                               
                            </div>
                            @endforeach
                            
                        </div>

@foreach($cate作为$cat)
@foreach($cate->Category as$sub)
@endforeach
@endforeach

我试图应用嵌套循环,以便可以看到父类别的子类别,但我遇到了错误

您的代码中有许多错误

根据您的迁移,外键的名称为
Parent\u id
,因此,如果此名称不遵循laravel约定,则需要在定义关系时指定确切的名称。另外,考虑到该关系可能返回多个子项,我建议您调整方法名称

### Category.php

public function children()
{
    return $this->hasMany(Category::class, 'Parent_id');
}
因此,在查询数据时,请执行以下操作:

$categories = Category::with('children')->get();

return view('index')->with('categories', $categories);
作为补充:子类别希望访问其父类别,因此您可以在模型中定义另一个方法

public function parent()
{
    return $this->belongsTo(Category::class, 'Parent_id');
}
通过这种方式,您可以获取父类别的信息:

$child = Category::find($id);
$parent = $child->parent;

我所做的错事是定义了一个应该被定义为

public function Category()
        {
            return $this->hasMany(self::class,'parent_id','id');
        }
为了获得父id,我必须使用

$category=Category::where('Parent_id','=','0')->get();
return view('index')->with('cate',$category);
而不是

return view('index')->with('cate',Category::all();