Select with Laravel / Eloquent

1

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 go through the foreach:

Undefined property: stdClass::$NM_MATRICULA_ALU

My view looks 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 resolve this?

Enjoying, is there a better way to do this select without needing a subquery? And how do I do it the way it is without being DB :: select raw?

Thank you for helping me!

    
asked by anonymous 14.08.2017 / 03:48

1 answer

2

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

Changes the first line of SQL to this:

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

    
14.08.2017 / 05:14