【问题标题】:Unlimited Nested Categories not working, Method Illuminate\Database\Eloquent\Collection::childrenRecursive does not exist无限嵌套类别不起作用,方法 Illuminate\Database\Eloquent\Collection::childrenRecursive 不存在
【发布时间】:2020-11-30 19:52:45
【问题描述】:

我在索引中也尝试了@foreach $categories->children(),仍然给出相同的错误,即 Method Illuminate\Database\Eloquent\Collection::childrenRecursive 不存在,无论我使用哪个函数,我都会得到相同的错误,我想显示所有类别及其子类别。好像不知道怎么弄

类别:

public function parent()
{
    return $this->belongsTo(Category::class, 'cat_id');
}
public function children()
{
    return $this->hasMany(Category::class, 'cat_id');
}
public function childrenRecursive()
{
   return $this->children()->with('childrenRecursive');
}

控制器:

 public function index()
    {
    $categories = Category::with('childrenRecursive')->whereNull('cat_id')->get();  
    return view('categories.index',compact('categories'));
    }

blade.php

@foreach ($categories->childrenRecursive() as $category)
          
                <tbody>
                <tr>
                    <td>
                        {{$category->name}}
                    </td>
                    <td data-cat-id="{{$category->id}}">
                        <!-- Button trigger modal -->
                        <button type="button" class="btn btn-primary add-subcat-modal-trigger">
                            Add Subcategory
                        </button>
                        <!-- Modal -->
                    </td>
                   
                    <td>
                        <form action="{{route('categories.destroy',$category->id)}}" method="POST">
                            @csrf
                            @method('DELETE')
                            <button class="btn btn-danger">Delete</button>
                        </form>
                    </td>
                </tr>
            @endforeach

【问题讨论】:

  • $categories 是一个包含模型的集合,它本身不是模型
  • 什么可以解决这个问题?

标签: laravel recursion


【解决方案1】:

关系定义似乎在逻辑上不正确。

Parent hasMany Children 和 Children belongsTo Parent 似乎合乎逻辑

public function parent()
{
    return $this->hasMany(Category::class, 'cat_id');
}

public function children()
{
    return $this->belongsTo(Category::class, 'cat_id');
}

//Defining a relationship with eagerloading would not work
//and on top of it you are trying to eager load childrenRecursive within childrenRecursive relation definition
public function childrenRecursive()
{
   return $this->children()->with('childrenRecursive');
}
@foreach ($categories as $cat)

    @foreach($cat->children as $category)
          
       <tbody>
            <tr>
                <td>{{$category->name}} </td>
                <td data-cat-id="{{$category->id}}">
                    <!-- Button trigger modal -->
                    <button type="button" class="btn btn-primary add-subcat-modal-trigger">
                        Add Subcategory
                    </button>
                    <!-- Modal -->
                </td>
                   
                <td>
                    <form action="{{route('categories.destroy',$category->id)}}" method="POST">
                        @csrf
                        @method('DELETE')
                         <button class="btn btn-danger">Delete</button>
                     </form>
                 </td>
             </tr>
        </tbody>
    @endforeach
@endforeach

【讨论】:

    猜你喜欢
    • 2020-11-21
    • 2021-09-08
    • 2020-11-13
    • 2019-12-11
    • 2020-02-04
    • 2019-11-12
    • 2019-05-24
    • 2019-09-04
    • 1970-01-01
    相关资源
    最近更新 更多