Perform a related table search on Laravel and page

Asked

Viewed 95 times

0

I would like to search all people of a certain class based on the class code using Laravel: Relacionamento

And right after paginate, to make a list.

Pupil

public function turmas()
{
    return $this->belongsToMany(Turma::class,'turma_classe', 'turma_id','aluno_id');
}

Gang

public function alunos()
{
    return $this->belongsToMany(Aluno::class,'turma_classe', 'aluno_id','turma_id');
}

Seeking

$aluno = Aluno::find(1);
$turmas = $aluno->turmas;

foreach($turmas as $turma)
{
    var_dump(Aluno::with(Turma::class)->where('codigo', $turma->codigo));
}
  • Are you using any code? and/or are you using Model? if yes you can provide in the question!

  • The correct question is: Search all Alunos of a certain Turma? that’s it?

  • I’ll update the question

  • Yes, using the code of Turma

1 answer

1


You already have the relationship ready in models, just use the navigation property alunos in the model Turma.

$turma = Turma::where('codigo', $codigoTurma)->first();

$alunos = $turma->alunos;

To page you can use the method paginate of Laravel, has almost everything necessary to make a pagination in it.

$registros = $alunos->paginate();

See more in the Laravel documentation.

  • I can accomplish the Turma:find($idTurma) using another column of the table, in the case codigo?

  • @ALE_ROM I edited the answer

Browser other questions tagged

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