我的数据库中有重复的任务,所以我的foreach中没有任何带有thir标签的重复任务
@foreach ($Tag->Tasks as $Task)
<li> Task :: {{ $Task->task_name }} </li>
@foreach ($Task->tasktags as $TaskTag)
<li> Task Tags :: {{ $TaskTag->name }} </li>
@endforeach
@endforeach
答案 0 :(得分:2)
在控制器中使用(在视图中插入参数之前)array_unique
函数
更多:http://php.net/manual/en/function.array-unique.php
如果您有收集对象: https://laravel.com/docs/master/collections#method-unique
答案 1 :(得分:0)
查看独特的方法here
@foreach ($Tag->Tasks as $Task)
<li> Task :: {{ $Task->task_name }} </li>
$uniqueTaskTags = $Task->tasktags->unique();
@foreach ($uniqueTaskTags as $TaskTag)
<li> Task Tags :: {{ $TaskTag->name }} </li>
@endforeach
@endforeach
答案 2 :(得分:0)
使用此选项,不会打印重复数据
{!! $Tag->Tasks = $Tag->Tasks->unique(); !!}
@foreach ($Tag->Tasks as $Task)
<li> Task :: {{ $Task->task_name }} </li>
{!! $Task->tasktags = $Task->tasktags->unique(); !!}
@foreach ($Task->tasktags as $TaskTag)
<li> Task Tags :: {{ $TaskTag->name }} </li>
@endforeach
@endforeach