'How to stop looping same data in foreach
So, I make categories for jawaban and if jawaban == 1 then jawaban input type should be a button radio and if jawaban == 2 the input type should be multiple choice but when i am looping the condition jawaban would be looping for all pertanyaans, I just want to loop only for that kegiatan
in blade.php
@foreach ($pertanyaans as $pertanyaan)
@if ($pertanyaan->kategori1 == '1')
<div class="form-group">
<input type="file" name="jawaban" id="jawaban" accept=".png, .jpeg, .jpg" data-allowed-file-extensions='["png", "jpeg", "jpg"]' id="input-file-now" class="dropify" data-max-file-size="3000K" />
<p class="help-block" style="font-size: 12px;">Max Filesize 3 MB (png, jpeg, jpg)</p>
</div>
@endif
@break($pertanyaan->kategori1 == 1)
@if ($pertanyaan->kategori1 == '2')
<div class="form-group">
<textarea class="form-control" id="jawaban" rows="3"></textarea>
</div>
@endif
@break($pertanyaan->kategori1 == 2)
@if ($pertanyaan->kategori1 == '3')
<div class="form-group" id="jawaban">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="pilihan" id="pilihan1" value="option1">
<label class="form-check-label" for="inlineRadio1">Sangat Tidak Sesuai</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="pilihan" id="pilihan2" value="option2">
<label class="form-check-label" for="inlineRadio2">Tidak Sesuai</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="pilihan" id="pilihan3" value="option1">
<label class="form-check-label" for="inlineRadio1">Lumayan Sesuai</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="pilihan" id="pilihan4" value="option2">
<label class="form-check-label" for="inlineRadio2">Sesuai</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="pilihan" id="pilihan5" value="option2">
<label class="form-check-label" for="inlineRadio2">Sangat Sesuai</label>
</div>
</div>
@endif
in my controller :
$list_pelaporan = PelaporanEfektivitas::all();
$kategori = kategori_pertanyaan::all();
$kegiatan = kegiatan::all();
$pertanyaans = PertanyaanEfektivitas::all();
if ($request->ajax()) {
return datatables()->of($list_pelaporan)
->addColumn('action', function ($data) {
$button = '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$data->id.'" data-original-title="Edit" class="edit btn btn-primary edit-post"><i class="fa fa-plus"></i> Pelaporan</a>';
$button .= ' ';
$button .= '<button type="button" name="detail" data-toggle="tooltip" name="detail" data-id="'.$data->id.'" class="detail btn btn-info detail-modal"><i class="fa fa-eye"></i> Detail</button>';
$button .= ' ';
$button .= '<button type="button" name="delete" id="'.$data->id.'" class="delete btn btn-danger"><i class="far fa-trash-alt"></i> Delete</button>';
return $button;
})
->rawColumns(['action'])
->addIndexColumn()
->make(true);
};
return view('pelaporan.efektivitas', compact('kategori','kegiatan','list_pelaporan', 'pertanyaans'));
Solution 1:[1]
You may want to use Switch Statements for multiple conditions
@switch($pertanyaan->kategori1)
@case(1)
// First case...
@break
@case(2)
// Second case...
@break
@case(3)
// Third case...
@break
@default
// Default case...
@endswitch
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lordisp |


