代码之家  ›  专栏  ›  技术社区  ›  Ali Özen

无法访问与AppServiceProvider的关系

  •  -1
  • Ali Özen  · 技术社区  · 6 年前

    在我的标题中,我需要使用一些关系。我在AppServiceProvider中定义了变量。就像这样:

    $v_categories = Category::where('parent_id', $v_group->id)->orderBy('order', 'asc')->get();
    $view->with('categories', $v_categories);
    

    在刀刃上,当我用前臂“分类”时,它的工作方式就像是一种魅力。 但当我试图将它与关系结合使用时,它抛出了一个异常。

        Exception
    Property [children] does not exist on this collection instance.
    

    这是我的关系:

    public function children()
    {
        return $this->hasMany('App\Category', 'parent_id');
    }
    

    我的问题是如何在刀片服务器中使用children函数和“categories”口才(来自AppServiceProvider)? 有个问题。但我不明白为什么?任何帮助都会很好。事先谢谢。

    1 回复  |  直到 6 年前
        1
  •  1
  •   simonhamp    6 年前

    hasMany get() Collection Category

    @foreach ($categories as $category)
        @foreach ($category->children as $child_category)
            {{ $child_category->name }}
        @endforeach
    @endforeach