@foreach ($programs as $program) //Looping to check the total number of Subactivities. If you have hasManyThrough Relation then this is not necessary @php $count = 0; @endphp @foreach($program->activities as $activity) @foreach($activity->subactivities as $subactivity) @php $count++ @endphp @endforeach @endforeach @php // Creating a Check for the number of Program $program_check = 0; @endphp @foreach($program->activities as $activity) @php // Creating a Check for the number of Activites $activity_check = 0; @endphp @foreach($activity->subactivities as $subactivity) //If number of program is greater than 0 which will increment at the end of subactivity loop then dont display the program again. This check will become 0 again when the next program loop begin. which is the first one @if ($program_check == 0) //Your Serial Number Counter. Replace it with your counter. {{ $counter++ }} {{ $program->name }} @endif //If number of activity greater than 0 which will increment at the end of subactivity loop then dont display the activity again. This check will become 0 again when the next activity loop begin. which is the second one @if ($activity_check == 0) {{ $activty->name }} @endif {{ $subactivites->name }} @php $program_check++; $activity_check++; @endphp @endforeach @endforeach @endforeach