Php 类别未按预期显示,有什么问题?
我有这样的分类: 我想为我已经成功创建的类别创建一个下拉菜单。但问题是,每当我单击链接时,子类别也会显示为父类别 请参见下图: 我想西部类应该只出现在服装类点击这是完美的作品,它不应该出现在点击类别链接 类别模型:Php 类别未按预期显示,有什么问题?,php,laravel-5,blade,Php,Laravel 5,Blade,我有这样的分类: 我想为我已经成功创建的类别创建一个下拉菜单。但问题是,每当我单击链接时,子类别也会显示为父类别 请参见下图: 我想西部类应该只出现在服装类点击这是完美的作品,它不应该出现在点击类别链接 类别模型: protected $fillable = [ 'name', 'parent_id' ]; public function products() { return $this->belongsToMany('App\Product')->wit
protected $fillable = [
'name', 'parent_id'
];
public function products()
{
return $this->belongsToMany('App\Product')->withTimestamps();
}
public function childs()
{
return $this->hasMany('App\Category', 'parent_id', 'id');
}
在控制器中:
$category = Category::where('parent_id', '!=', '0')->with('childs')->get();
观点:
<li class="dropdown">
<a href="javascript:void(0)" class="dropdown-toggle links-titilium" data-toggle="dropdown" role="button" aria-expanded="false">Categories <span class="caret"></span></a>
<ul class="dropdown-menu multi-level" role="menu">
@foreach( $category as $cat )
<li @if($cat->childs->count()) class="dropdown-submenu" @endif>
<a class="links-titilium" href="{{ url( '/store/category', [$cat->id, Safeurl::make($cat->name)] ) }}" @if( $cat->childs->count() ) class="dropdown-toggle" data-toggle="dropdown" @endif>
{{ $cat->name }}
</a>
@if( $cat->childs->count() )
<ul class="dropdown-menu" role="menu">
<li>
@foreach( $cat->childs as $child )
<a href="{{url('/category', [Safeurl::make($cat->name), Safeurl::make($child->name)])}}">
{{ $child->name }}
</a>
@endforeach
</li>
</ul>
@endif
</li>
@endforeach
</ul>
</li>
根据您的表格,行上方的行将获取所有类别。要获取级别为1的类别,请尝试下面的行
$category = Category::where('parent_id', '1')->with('childs')->get();
- Apparels
- Clothes
-- Western
或者获取具有子类别的顶级类别
$category = Category::where('parent_id', null)->with('childs')->get();
- None
- Apparels
- Clothes
-- Western
我会尝试在第一个foreach循环之后创建一个条件。它必须对整个li进行englobe处理,以便在数组位于子索引处时不会创建元素
@foreach( $category as $cat )
@if($cat->parent_id == 1) /*this is the beginning of the if statement*/
<li @if($cat->childs->count()) class="dropdown-submenu" @endif>
<a class="links-titilium" href="{{ url( '/store/category', [$cat->id, Safeurl::make($cat->name)] ) }}" @if( $cat->childs->count() ) class="dropdown-toggle" data-toggle="dropdown" @endif>
{{ $cat->name }}
</a>
@if( $cat->childs->count() )
<ul class="dropdown-menu" role="menu">
<li>
@foreach( $cat->childs as $child )
<a href="{{url('/category', [Safeurl::make($cat->name), Safeurl::make($child->name)])}}">
{{ $child->name }}
</a>
@endforeach
</li>
</ul>
@endif
</li>
@endif /*this is the end of the if statement*/
@endforeach`
谢谢你的答复,先生。但这不是我想要的。我需要获取所有类别,包括除None类别以外的child。如果存在子类别,则不应将其显示为父类别。将立即解决您的问题。
@foreach( $category as $cat )
@if($cat->parent_id == 1) /*this is the beginning of the if statement*/
<li @if($cat->childs->count()) class="dropdown-submenu" @endif>
<a class="links-titilium" href="{{ url( '/store/category', [$cat->id, Safeurl::make($cat->name)] ) }}" @if( $cat->childs->count() ) class="dropdown-toggle" data-toggle="dropdown" @endif>
{{ $cat->name }}
</a>
@if( $cat->childs->count() )
<ul class="dropdown-menu" role="menu">
<li>
@foreach( $cat->childs as $child )
<a href="{{url('/category', [Safeurl::make($cat->name), Safeurl::make($child->name)])}}">
{{ $child->name }}
</a>
@endforeach
</li>
</ul>
@endif
</li>
@endif /*this is the end of the if statement*/
@endforeach`