循环重复相同的数据8次或更多次

时间:2018-04-04 05:27:52

标签: php mysql laravel-5

我正在使用foreach循环来显示表上的数据,但不知道是什么导致相同的数据在循环中重复8次或更多次。

例如,带id = 1的列重复8次,id = 2列将显示8次,依此类推。我已经尝试了很多,但似乎我最后给出的连接查询有一些问题

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>

这是我用于循环名称'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();

1 个答案:

答案 0 :(得分:1)

使用group by

尝试此查询
dialog