Faiez
Faiez

Reputation: 305

Loop is repeating same data 8 or more times

I am using the foreach loop to show data on a table but don't know what is causing the same data to be repeated 8 or more times in the loop.

For example, Column with id = 1 is repeated 8 times then column with id = 2 will be displayed 8 times and so on. I have tried a lot but it seems it has some problem with the join query I have given in the end

Blade.php

                 <table class="table table-hover" id="example">
                        <thead>
                        <tr>
                            <th width="">Sr#</th>
                            <th width="">Leave Policy</th>
                            <th width="">Read</th>
                            <th width="">Allowed</th>
                            <th width="">Applied</th>
                            <th width="">Approved</th>
                            <th width="">Available</th>
                            <th width="">Attachment</th>
                            <th width="">Conflict</th>
                            <th width="">Action</th>
                            <th width="">Comment</th>
                        </tr>
                        </thead>
                        @foreach($join as $joins)

                        <tbody id="myTable">
                        <tr>
                            <td class="text-center">{{ $joins->id }}</td>
                            <td  id="policy" >
                                {{ $joins->leave_policy }}

                            </td>
                            @if( $joins->leave_policy=='Urgent Leave')
                            <td><a href="\tms\tms-live\public\files\Urgent Leave.pdf" target="_blank" class="attachment-icon-pdf mr-5"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a></td>
                            @elseif( $joins->leave_policy=='Annual')
                            <td><a href="\tms\tms-live\public\files\Annual Leave.pdf" target="_blank" class="attachment-icon-pdf mr-5"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a></td>
                            @elseif( $joins->leave_policy=='Public / National holidays')
                            <td><a href="\tms\tms-live\public\files\Public and National Holiday.pdf" target="_blank" class="attachment-icon-pdf mr-5"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a></td>
                            @elseif( $joins->leave_policy=='Government Allowed Leave')
                            <td><a href="\tms\tms-live\public\files\Gazetted Holidays.pdf" target="_blank" class="attachment-icon-pdf mr-5"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a></td>
                            @elseif( $joins->leave_policy=='Maternity Leave')
                            <td><a href="\tms\tms-live\public\files\Maternity Leaves.pdf" target="_blank" class="attachment-icon-pdf mr-5"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a></td>
                            @elseif( $joins->leave_policy=='Paternal Leaves')
                            <td><a href="\tms\tms-live\public\files\Paternal Leaves.pdf" target="_blank" class="attachment-icon-pdf mr-5"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a></td>
                            @elseif( $joins->leave_policy=='Bereavement Leaves')
                            <td><a href="\tms\tms-live\public\files\Bereavement Leaves.pdf" target="_blank" class="attachment-icon-pdf mr-5"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a></td>
                            @elseif( $joins->leave_policy=='Wedding Leaves')
                            <td><a href="\tms\tms-live\public\files\Wedding Leaves.pdf" target="_blank" class="attachment-icon-pdf mr-5"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a></td>
                            @endif
                            <td>{{ $joins->total_no_of_leaves_allowed_per_year }}</td>
                            <td>3</td>
                            <td>1</td>
                            <td></td>
                            <td class="text-center">
                                @if($joins->attach == "None")
                                    None
                                @else
                                <a href="{{url('public/attachment',$joins->attach)}}" id="ext" class="attachment-icon-pdf" target="_blank"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a>
                                @endif
                            </td>
                            <td></td>
                            <td>
                                @if($joins->leave_status == 'Pending')
                                    No
                                @else
                                    Yes
                                @endif
                            </td>
                            <td>
                                <a class="edit-modal approve-icon">
                                    <i class="fa fa-check" aria-hidden="true"></i>
                                </a>
                            </td>
                        </tr>

                        </tbody>
                        @endforeach

                    </table>

And this the query I am using for loop name 'join'

$join = DB::table('leaves_policy')
    ->join('leaves_policies', 'leaves_policy.leave_policy', '=', 'leaves_policies.title')
    ->join('leaves_requests', 'leaves_policy.requested_by' , '=', 'leaves_requests.requested_by')
    ->select('leaves_policy.*', 'leaves_policies.title', 'leaves_policies.total_no_of_leaves_allowed_per_year',
        'leaves_policies.no_of_months_leaves_valid', 'leaves_policies.max_leaves_per_month', 'leaves_policies.max_con_leaves_per_month',
        'leaves_requests.leave_status')
    ->get();

Upvotes: 0

Views: 55

Answers (1)

Rp9
Rp9

Reputation: 1963

try this query with group by

$join = DB::table('leaves_policy')
    ->join('leaves_policies', 'leaves_policy.leave_policy', '=', 'leaves_policies.title')
    ->join('leaves_requests', 'leaves_policy.requested_by' , '=', 'leaves_requests.requested_by')
    ->select('leaves_policy.*', 'leaves_policies.title', 'leaves_policies.total_no_of_leaves_allowed_per_year',
        'leaves_policies.no_of_months_leaves_valid', 'leaves_policies.max_leaves_per_month', 'leaves_policies.max_con_leaves_per_month',
        'leaves_requests.leave_status')
    ->groupBy('leaves_policy.id')    
    ->get();

Upvotes: 1

Related Questions