-1
I have a Course and this course has 1 or several Modules, and each Module has 1 or several disciplines. In my controller I did so:
$_modSelecionados = Modulo::select('id_modulo')->where('id_curso', $curso)->get();
$modSelecionados = [];
foreach ($_modSelecionados as $modulo) {
$modSelecionados[] = $modulo->id_modulo;
// Pega as Disciplinas do Módulo
$_discSelecionadas = DisciplinaModulo::select('id_disciplina')->where('id_modulo', $modSelecionados)->get();
$discSelecionadas = [];
foreach ($_discSelecionadas as $disciplina) {
$discSelecionadas[] = $disciplina->id_disciplina;
}
}
I passed everything I need to View and rode like this:
@foreach($modulos as $modulo)
@if(in_array($modulo->id_modulo, $modSelecionados) ? 'selected' : '')
<table>
<thead>
<tr>
<th scope="col"><i class="fal fa-long-arrow-down"></i>{{ $modulo->nome }}</th>
<th scope="col">Carga Horária</th>
</tr>
</thead>
<tbody>
@foreach($disciplinas as $disciplina)
@if(in_array($disciplina->id_disciplina, $discSelecionadas) ? 'selected' : '')
<tr>
<td style="width: 65%;">{{ $disciplina->nome }}</td>
<td>{{ $disciplina->carga_horaria }} horas</td></tr>
@endif
@endforeach
</tbody>
</table>
@endif
@endforeach
What is happening is that it generates a table for each MODULE, but repeats the DISCIPLINES of module 1 for module 2. It does not seek the disciplines of Module 2. As in the image below:
How to solve this???
I did not understand who voted negative for the question, it shows an obvious doubt of a beginner in the framework and should be respected because it does not hurt the guidelines of stackoverflow
– Ademir Mazer Jr - Nuno