【问题标题】:Laravel @foreach RepeatingLaravel @foreach 重复
【发布时间】:2021-04-17 17:03:19
【问题描述】:

当我尝试添加 @foreach 3 次时,它会重复我的数据。我该如何解决?

这是我的代码

我的控制器

$students = Student::with('FeeScheem','IndFeeScheem','FeeGroup','FeeGroup1')
->where('students.stu_id', $students)
->get();
$this->students = $students;

雄辩的关系

public function FeeScheem()
        {
            return $this->hasOne(FeeScheem::class, 'fs_id', 'stu_fee_scheem_id'); 
        }

        public function IndFeeScheem()
        {
            return $this->hasOne(IndividuallyFeeScheem::class, 'id', 'stu_ind_fee_scheem_id');
        }

        public function FeeGroup()
        { 
            return $this->hasManyThrough(FeeMaster::class, FeeGroups::class);        
        }

        public function FeeGroup1()
        { 
            return $this->hasMany(FeeGroups::class, 'individually_fee_scheem_id', 'stu_fee_scheem_id');        
        }

我的刀片文件

@foreach ($students as $stu=>$student)
    @foreach ($student->FeeGroup as $key=>$FeeG)
  
    @foreach ($student->FeeGroup1 as $key1=>$FeeG1)
    <tr>
      <td>{{ $loop->iteration }}</td>
      <td>{{ $FeeG1->fee_group_name }}</td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    @endforeach 
    @endforeach 
    @endforeach 
  </tbody>

这是预览

Click here to preview

【问题讨论】:

    标签: laravel foreach


    【解决方案1】:

    您可以使用loop variableindex 属性:

    @foreach ($students as $stu=>$student)
      @if($loop->index < 2)
          // Your code
      @endif
    @endforeach
    

    【讨论】:

      【解决方案2】:

      您错误地关闭了标记。应该是这样的:

      @foreach ($students as $stu=>$student)
          @foreach ($student->FeeGroup as $key=>$FeeG)
              <tr>
              <td></td>
            </tr>
          @endforeach 
      
          @foreach ($student->FeeGroup1 as $key1=>$FeeG1)
          <tr>
            <td>{{ $FeeG1->fee_group_name }}</td>
            <td></td>
          </tr>
          @endforeach 
      @endforeach 
        </tbody>
      

      【讨论】:

        猜你喜欢
        • 2018-11-18
        • 2021-10-23
        • 2020-09-23
        • 2021-02-24
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        • 2017-09-17
        相关资源
        最近更新 更多