如何解决Blade Recursive,增加填充深度
我想在孩子每次进入项目时增加深度 int
,以便我可以填充 a
标签。
以下是我的代码,我认为这会起作用,但是即使我在 $items
内的其中一项项目中有孩子,该数字仍为 1。
sidebar.blade.PHP
<nav class="flex flex-col mt-10">
<ul class="pl-4">
@foreach($items as $item)
<x-layouts.sidebar.items :item="$item"></x-layouts.sidebar.items>
@endforeach
</ul>
</nav>
sidebar.items.blade.PHP
<li>
@if(count($item) > 0)
<a href="{{ $item['href'] }}" class="focus:text-blue-500 dark-focus:text-blue-400 focus:outline-none w-full transition duration-500 ease-in-out pl-4 py-2 text-gray-700 dark:text-gray-400 hover:bg-blue-200 dark-hover:bg-blue-500 transition duration-500 ease-in-out block">
{{ $item['text'] }} {{ $depth }}
</a>
@if (count($item['children']) > 0)
<ul class="pl-4">
@foreach($item['children'] as $child)
<x-layouts.sidebar.items :item="$child" :depth="$loop->parent->index"></x-layouts.sidebar.items>
@endforeach
</ul>
@endif
@else
<hr>
@endif
</li>
Sidebar\Items.PHP
<?PHP
namespace App\View\Components\Layouts\Sidebar;
use Illuminate\View\Component;
use Illuminate\View\View;
class Items extends Component
{
/**
* @var array
*/
public $item;
/**
* @var int
*/
public $depth;
/**
* Create a new component instance.
*
* @param array $item
* @param int $depth
*/
public function __construct($item = [],$depth = 1)
{
$this->item = $item;
$this->depth += $depth;
}
/**
* Get the view / contents that represent the component.
*
* @return View|string
*/
public function render()
{
return view('components.layouts.sidebar.items');
}
}
数据:
$this->items = [
[ 'href' => '/home','text' => 'Home','children' => [],'active' => 'border-l-2 border-blue-500' ],[ 'href' => 'javascript:void(0)','text' => 'Test','active' => '' ],'text' => 'Websites',[],[
'href' => '/administration','text' => 'Administration','children' => [
[
'href' => 'javascript:void(0)','text' => 'Locked Devsites','active' => '','children' => []
]
],'active' => ''
],'text' => 'Documentation','text' => 'logout','active' => '' ]
];
结果:
Home 1
Test 1
Websites 1
Websites 1
Administration 1
Locked Devsites 5
Documentation 1
logout 1
解决方法
如果我说对了你想要实现的目标,你甚至不需要 $depth
属性,因为刀片的 @foreach
指令有它自己的深度属性,它告诉你你是如何嵌套的。
在您的代码中,改为使用:
:depth="$depth"
使用:
:depth="$loop->depth"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。