将 parent 作为选项组 header 以及 children 作为选项 - Laravel
Make parent as the option group header along with the children as the options - Laravel
我有很多通过 eloquent 的关系,在 Categories->SubCategories->MenuItems
之间。我需要将类别名称显示为 optgroup header 及其子类别作为选项值。它显示了每个 sub-category 上方的类别,而不是我的期望。抱歉我是菜鸟,请帮助我。
这是我到目前为止所做的。
<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>
这是我得到的输出。
编辑
这是我的亲戚。
类别模型
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');
}
菜单项模型
public function SubCategories() {
return $this->belongsTo(SubCategory::class, 'sub_category_id');
}
您实际上是在尝试显示类别名称作为选项组的所有子类别header。您的解决方案是从组件发送类别,然后循环它们以获取子类别。
在组件中
//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]);
}
然后在查看循环类别中,然后是子类别。
<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>
我有很多通过 eloquent 的关系,在 Categories->SubCategories->MenuItems
之间。我需要将类别名称显示为 optgroup header 及其子类别作为选项值。它显示了每个 sub-category 上方的类别,而不是我的期望。抱歉我是菜鸟,请帮助我。
这是我到目前为止所做的。
<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>
这是我得到的输出。
编辑
这是我的亲戚。
类别模型
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');
}
菜单项模型
public function SubCategories() {
return $this->belongsTo(SubCategory::class, 'sub_category_id');
}
您实际上是在尝试显示类别名称作为选项组的所有子类别header。您的解决方案是从组件发送类别,然后循环它们以获取子类别。
在组件中
//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]);
}
然后在查看循环类别中,然后是子类别。
<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>