Laravel 5.2 - Relationship Many To Many between records of the same table

Asked

Viewed 465 times

1

The tables:

  • The table users, records users who may have N functions that are stored in the table funcoes (student, responsible, teacher, etc).
  • The table funcao_user is pivot that makes the relationship Many To Many users and funcoes.

So far, so good.

A user who has a student role can have N users with the responsibility role and, a user who has the responsibility function to have N users with the student role, that is, there is a relationship Many To Many within the users table.

For this relationship, I created the pivot table aluno_responsavel with the fields: aluno_id, responsavel_id and parentesco (in relation to parent, indicates parent, etc):

 Schema::create('aluno_responsavel', function (Blueprint $table) {
     $table->integer('aluno_id')->unsigned();            
     $table->integer('responsavel_id')->unsigned();
     $table->string('parentesco');
     $table->foreign('aluno_id')->references('id')->on('users')->onDelete('cascade');
     $table->foreign('responsavel_id')->references('id')->on('users')->onDelete('cascade');
});

How the relationship between the tables would be users and aluno_responsavel in the User model?

  • Only do referencesMany on both sides for the User Model

  • Poxa ... is complicated by the way, aluno_id and responsavel_id are pointed to the same table and the same field, for me a wrong normalization there ...

  • 1

    Thank you for participating, @Virgilionovic and @gmsantos! I found an argument about the same case with information that’s pretty close to what I’m looking for, but I haven’t been able to test that information yet. When I test I inform the result. ---> https://github.com/laravel/framework/issues/441

1 answer

0

With the information found on https://github.com/laravel/framework/issues/441 I did the following relationship on the model and it worked (to simplify the tests, I left out the kinship column):

public function responsaveis()
{
     return $this->belongsToMany('App\User', 'aluno_responsavel', 'aluno_id', 'responsavel_id');
}

With this, I can store the ids aluno_id and responsavel_id with the following controller command:

$user->responsaveis()->attach($request->responsavel_id);

But, I still need to know how to recover the values stored in pivot table.
I made this attempt, but it does not bring the expected data:

$userAluno = User::find($user_id);

foreach ($userAluno->responsaveis as $responsavel) {
     echo $responsavel->responsavel_id . <br />;
}
  • Have you created a model for Responsible and another for Users? How are your models?

  • Hi @Evert! I did not make a model for Responsavel because there is no responsible table, only the aluno_responsavel pivot table. The User model looks like this: public Function funcoes() { Return $this->belongsToMany('App Funcao'); } public Function responsaveis() { Return $this->belongsToMany('App User', 'aluno_responsavel', 'aluno_id', 'responsavel_id'); }

Browser other questions tagged

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