Create foreach with another foreach result

Asked

Viewed 39 times

-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:

inserir a descrição da imagem aqui

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

1 answer

0


In its logic the problem is to pass the entire collection of disciplines without distinguishing what the relationship with each module.

Anyway, this is not the best way to work with Laravel and Eloquent.

Change the code in your templates to insert the relationships as described in the documentation. Here is an example:

class Modulo extends Model {
    // função com o relacionamento
    public function disciplinas() {
        // se o id chave estrangeira na tabela disciplina para modulo estiver
        // nos padrões descritos na documentação do Laravel
        return $this->hasMany(Disciplina::class);
    }
}

In the controller

$modulos = Modulo::where('id_curso', $curso)->get();

In view

@foreach($modulos as $modulo)
    <hr/>
    <p>{{ $modulo->nome }}</p>
    <p>Disciplinas</p>
    @foreach ($modulo->disciplinas as $disciplina)
        <p>{{ $disciplina->nome }}</p>
    @endforeach
@foreach

The example is simplified, but it is only to demonstrate the relationship between the models and how to recover using the "way" Eloquent.

In this way, each module returns its disciplines directly by the property described in its model class.

  • In this case, the Disciplines are in the Disciplina_modulo grid. In the table Discipline I have her data, in the table Module I have his data and the Course. But the list of disciplines for each module is in the Disciplina_modulo link table. What changes? How do I get the name of the Discipline?

  • 1

    Mazer, I figured it out! I did so in the Module Model: Return $this->belongsToMany("App Models Admin Discipline", 'disciplina_modulo', 'id_modulo', 'id_disciplina'); . Thank you so much for the help friend, and thank you also for commenting on the question, really is my first project in Laravel and I’m picking up for a few things. Thank you very much

  • Great, glad I could help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.