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

标签 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();

最佳答案

尝试使用分组依据进行此查询

$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();

关于php - 循环重复相同的数据 8 次或更多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49643419/

相关文章:

laravel - 如何获得其他用户撰写的用户提交的评价

php - 为什么 "104"排在 "0000105"之前?

java - JSP中图片上传问题

进行全文搜索时Mysql语法错误

php - 这段 php 代码有什么问题,它不会插入数据?

laravel - Laravel:如何在PhpUnit上启用stacktrace错误

php - 在 Laravel 中展平数组?

php - 从 MySQL 查询构建数组以格式化 json

php - SQL:条件语法错误和 mysql_fetch_object 误用

php - 合并两个数组,当前导子字符串相同时用第二个数组值覆盖第一个数组值