Prevent Foreach To return Duplicated Tasks

Posted 7 months ago by Kareimovich

hi, everybody, I'm trying to Prevent To return duplicated Tasks in relation

A Path has many Tags, every Tag has Many Tasks, every Task has many tags

My Code Prevent to return duplicated Tasks in Tag but I need to Prevent Duplicated Tasks in All Tasks of Path

My Controller

    $Paths = Path::with(['pathtags' => function ($q) use ($TagArray)
     {$q->with(['Tasks' => function ($q) use ($TagArray) {$q->has('tasktags', '=', 2)
            ->with('tasktags');
        }]);
    }])->where('id', '=', 1)->get();

My Blade

  @foreach ($Paths as $Path)
      <h2> {{$Path->name}}</h2>
      @foreach ($item->pathtags as $Tag)

        @php $a=array(); @endphp 
            @foreach ($Tag->Tasks as $Task)
               @if (!in_array($Task->task_name,$a))
                  <li> Task :: {{ $Task->task_name }} </li>
                 @php
                    array_push($a,$Task->task_name);
                @endphp
               @else 
       <li> Task :: Duplicated </li>
      @endif 

@foreach ($a as $item)
   <li>Array Task :::  {{$item}}</li>
@endforeach

   
 @endforeach 
    @endforeach 
        @endforeach

ScreenShot For Ouput of blade

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.