用口才选择时重复数据

2022-04-10 00:00:00 php laravel laravel-blade
我正在尝试创建this question's项目的详细信息页面。当我尝试获取数据时,它复制了47次(每个报告有47个数据)like this。我只需要一行中的一个数据,然后是下一行中的第二个数据,依此类推。这也发生在编辑页面上。

我的观点:

@foreach($sasarans as $i => $sasaran)
   @foreach($sasaran->indikator as $indikator)
     @foreach($indikator->pertanyaan as $pertanyaan)
     <tr>
       @if($loop->first)
         <td style="text-align:center; vertical-align:middle;" rowspan="{{$indikator->pertanyaan->count()}}">{{$i+1}} </td>
         <td style="vertical-align:middle;" rowspan="{{$indikator->pertanyaan->count()}}">{{$sasaran->sasaran}} </td>
          <td style="vertical-align:middle;" rowspan="{{$indikator->pertanyaan->count()}}">{{ $indikator->indikator}} </td>
       @endif
           <td style="vertical-align:middle;">{{ $pertanyaan->pertanyaan}}</td>
            @foreach($data_laporans as $data)
               <td>{{$data->jumlah}}</td>
               <td>{{$data->keterangan}}</td>
               <td><a href="{{ route('data.download', $data->id) }}" class="btn btn-sm btn-default btn-block"><i class="fa fa-download"></i> Unduh Bukti</a></td>
             @endforeach
        </tr>
     @endforeach
   @endforeach
@endforeach

控制器:

 public function show($id)
{
    $sasarans = Sasaran::all();
    $laporan_indikators = LaporanIndikator::where('id',$id)->first();
    $data_laporans = DataLaporan::where('id_laporan',$id)->get();

    return view('indikator-kinerja.show',compact('laporan_indikators','data_laporans','sasarans'));
}

我还尝试在phpMyAdmin中执行查询,没有重复数据。Data in PhpMyAdmin

您可以在this question中查看模型关系和其他详细信息。我为我糟糕的英语感到抱歉。请帮帮我,因为我还是初学者。谢谢。


解决方案

尝试了几次后,实际上我只需要更改视图:

 @foreach($sasarans as $i => $sasaran)
   @foreach($sasaran->indikator as $indikator)
     @foreach($indikator->pertanyaan as $pertanyaan)
     <tr>
       @if($loop->first)
         <td style="text-align:center; vertical-align:middle;" rowspan="{{$indikator->pertanyaan->count()}}">{{$i+1}} </td>
         <td style="vertical-align:middle;" rowspan="{{$indikator->pertanyaan->count()}}">{{$sasaran->sasaran}} </td>
          <td style="vertical-align:middle;" rowspan="{{$indikator->pertanyaan->count()}}">{{ $indikator->indikator}} </td>
       @endif
           <td style="vertical-align:middle;">{{ $pertanyaan->pertanyaan}}</td>
            @foreach($pertanyaan->data_laporan as $data)
               <td>{{$data->jumlah}}</td>
               <td>{{$data->keterangan}}</td>
               <td><a href="{{ route('data.download', $data->id) }}" class="btn btn-sm btn-default btn-block"><i class="fa fa-download"></i> Unduh Bukti</a></td>
             @endforeach
        </tr>
     @endforeach
   @endforeach
@endforeach

相关文章