将父项作为选项组标头,将子项作为选项-Laravel [英] Make parent as the option group header along with the children as the options - Laravel

查看:36
本文介绍了将父项作为选项组标头,将子项作为选项-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Categories-> SubCategories-> MenuItems 之间有着许多通俗易懂的关系.我需要将类别名称显示为optgroup标头,并将其子类别显示为选项值.而不是我的期望,它显示了每个子类别之上的类别.很抱歉成为菜鸟,请帮助我.

I have a has many through eloquent relationship, between Categories->SubCategories->MenuItems. I need to display a category name as the optgroup header and its subcategories as the option value. Instead of my expectations, its showing the category above each and every sub-category. Sorry for being a noob, please help me.

这是我到目前为止所做的.

Here's what I have done so far.

<select wire:model="sub_category_id" class="form-select block w-full mt-1">
    <option>Select Sub Category</option>
    @foreach ($menuitemlist as $menuitem)
        <optgroup label="{{ $menuitem->SubCategories->Categories->category_name }}">
            <option value="{{ $menuitem->SubCategories->id }}">{{ $menuitem->SubCategories->sub_category_name }}</option>
        </optgroup>
    @endforeach
    @error('sub_category_id') <h1 class="text-red-500">{{$message}}</h1>@enderror
</select>

这是我得到的输出.

这是我的关系.
类别模型

Here are my relations.
Category Model

public function SubCategories() {
   return $this->hasMany(SubCategory::class, 'category_id');
}

public function MenuItems() {
    return $this->hasManyThrough(MenuItem::class, SubCategory::class, 'category_id', 'sub_category_id');
}

子类别模型

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

public function MenuItems() {
    return $this->hasMany(MenuItem::class, 'sub_category_id');
}

MenuItem模型

MenuItem Model

public function SubCategories() {
    return $this->belongsTo(SubCategory::class, 'sub_category_id');
}

推荐答案

您实际上是在尝试显示所有具有类别名称作为选项组标题的子类别.您的解决方案是从组件中发送类别,然后循环它们以获取子类别.

you are actually trying to show all sub categories with category name as the option group header. your solution would be sending the categories from the component and then loop them to get sub categories.

在组件中

//don't forget to call Category Model
use App\Models\Category;
public function render()
{
    $searchParams = '%'.$this->search.'%';
    $menuitemlist = MenuItem::where('item_name','like', $searchParams)->latest()->paginate(5);
    $categories = Category::with('SubCategories')->get();

    return view('livewire.master-menu.menu-item-list', ['menuitemlist' => $menuitemlist, 'categories' => $categories]);
}

,然后是视图循环中的类别,然后是子类别.

and then in view loop categories first and then the sub categories.

<select wire:model="sub_category_id" class="form-select block w-full mt-1">
    <option>Select Sub Category</option>
    @foreach ($categories as $category)
       <optgroup label="{{ $category->category_name }}">
           @foreach ($category->SubCategories as $sub)
               <option value="{{ $sub->id }}">{{ $sub->sub_category_name }}</option>
           @endforeach
       </optgroup>
    @endforeach
    @error('sub_category_id') <h1 class="text-red-500">{{$message}}</h1>@enderror
</select>

这篇关于将父项作为选项组标头,将子项作为选项-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆