在laravel或livewire中获取子类别

在laravel或livewire中获取子类别,laravel,laravel-livewire,Laravel,Laravel Livewire,我正在使用Livewire版本2。我想在网站上显示所有类别。 母亲类别=0 这些类别的子集是parent\u id=1。我怎样才能得到这个子集 关系 例如: 到目前为止,我有一个类别叫电视,好吗 我想将三星或LG类别添加到此电视 产品类别模型: class HeaderBottom extends Component { public $category; public function mount(ProductCategory $category) {

我正在使用Livewire版本2。我想在网站上显示所有类别。 母亲类别=0 这些类别的子集是
parent\u id=1
。我怎样才能得到这个子集 关系

例如: 到目前为止,我有一个类别叫电视,好吗 我想将三星或LG类别添加到此电视

产品类别模型

class HeaderBottom extends Component
{
    public $category;

    public function mount(ProductCategory $category)
    {
        $this->category = $category;
    }

    public function render()
    {
        $a = 1;
        $allCategories = ProductCategory::where('parent_id', '=', 0)->latest()->get();
        $subcategory = ProductCategory::where('parent_id', '=', $a)->latest()->get();

        return view('livewire.site.layout.header.header-bottom', compact(['allCategories','subcategory']));
    }
}
公共函数父项名称($id)
{
$name_parent=ProductCategory::where('id',$id)->first();
如果(!$name\u家长){
返回';
}否则{
$name\u parent=$name\u parent->topic;
返回$name\u父项;
}
}
Livewire组件

class HeaderBottom extends Component
{
    public $category;

    public function mount(ProductCategory $category)
    {
        $this->category = $category;
    }

    public function render()
    {
        $a = 1;
        $allCategories = ProductCategory::where('parent_id', '=', 0)->latest()->get();
        $subcategory = ProductCategory::where('parent_id', '=', $a)->latest()->get();

        return view('livewire.site.layout.header.header-bottom', compact(['allCategories','subcategory']));
    }
}
我的刀片

class HeaderBottom extends Component
{
    public $category;

    public function mount(ProductCategory $category)
    {
        $this->category = $category;
    }

    public function render()
    {
        $a = 1;
        $allCategories = ProductCategory::where('parent_id', '=', 0)->latest()->get();
        $subcategory = ProductCategory::where('parent_id', '=', $a)->latest()->get();

        return view('livewire.site.layout.header.header-bottom', compact(['allCategories','subcategory']));
    }
}

    {{--
  • --}} @foreach($ALLCOREGRIES作为$category)
  • @endforeach
  • @foreach($子类别作为$项) @endforeach

也许您可以使用
wire:单击
设置子类别


class HeaderBottom扩展组件
{
公帑类别;;
公共$subcategories=[];
公共功能挂载(ProductCategory$category)
{
$this->category=$category;
}
公共职能
{
$allCategories=ProductCategory::where('parent_id','=',0)->latest()->get();
返回视图('livewire.site.layout.header.header-bottom',compact('allCategories');
}
用于($parent\u id)的公共函数GetSubcategories
{
$this->subcategories=ProductCategory::where('parent_id',$parent_id)->latest()->get()->all();
}
}