Select with Laravel/Eloquent

Asked

Viewed 2,431 times

2

I have the following select in my controller:

$turmaAlunos = DB::select('
    SELECT alunos.ST_ALUNO_ALU, alunos.ID_ALUNO_ALU
    FROM alunos WHERE alunos.ID_ALUNO_ALU NOT IN (
        SELECT turma_alunos.ID_ALUNO_ALU
        FROM turma_alunos
        INNER JOIN turmas ON turma_alunos.ID_TURMA_TUR = turmas.ID_TURMA_TUR
        WHERE turma_alunos.ID_TURMA_TUR = ?
    )
', [$id]);

It returns me what I need, however as stdClass and this is causing me the following error in the view when I try to traverse with foreach:

Undefined property: stdClass::$NM_MATRICULA_ALU

My view is like this:

@foreach($turmaAlunos as $turmaAluno)
    <tr>
         <td><input type="checkbox" name="alunos[]" value="{{ $turmaAluno->ID_ALUNO_ALU }}"></td>
         <td>{{ $turmaAluno->NM_MATRICULA_ALU }}</td>
         <td>{{ $turmaAluno->ST_ALUNO_ALU }}</td>
     </tr>
 @endforeach

How can I fix this?

Taking advantage, is there a better way to do this select without needing a subquery? And how do I do the way it is without being with DB::select brute?

I appreciate anyone who can help me!

1 answer

2


You need to put the column NM_MATRICULA_ALU in your bank consultation because it is not being returned.

Change the first line of SQL to this:

SELECT alunos.ST_ALUNO_ALU, alunos.ID_ALUNO_ALU, alunos.NM_MATRICULA_ALU

  • Primary error this mine :/ Thank you very much!

Browser other questions tagged

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