Support the ongoing development of Laravel.io →
Database Eloquent

Hi ! I have a table named professor_turma, which has some importante keys: ´cod_professor, cod_disciplina, cod_turma`.

cod_professor as FK from Professor table. cod_disciplina as FK from Disciplina table. cod_turma references the cod_turma from Turma table.

MY actuall code brings me the name of every professor, but I cant get all the Turmas that professor has in professor_turma table.

class Professor extends Model
{
    protected $table = "professor";
    protected $primaryKey = 'cod_professor';

    public function turmas()
    {
        return $this->belongsToMany('App\Turma', 'professor_turma','cod_professor', 'cod_turma')->withPivot('CH','ano_semestre');
    }

How may I get all the records inside the professor_turma and also the name of the professor from Professor table ?

How may I do something like this:

SELECT DISTINCT professor.nome, professor_turma.cod_professor, professor_turma.cod_turma, professor_turma.cod_disciplina, turma.nome_disciplina
FROM professor_turma
INNER JOIN professor ON professor_turma.cod_professor = professor.cod_professor
INNER JOIN turma ON professor_turma.cod_turma = turma.cod_turma AND professor_turma.cod_disciplina = turma.cod_disciplina
WHERE professor_turma.cod_professor =  ?

Maybe create the model of professor_turma and from this one I make the joins?? How could I specify the joins?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.