Relationship N to N with 4 tables in Laravel 5.6

Asked

Viewed 341 times

2

Good afternoon, you guys, I started using the framework Laravel a little while ago and I’m having difficulties in implementing a relationship N/N using 4 different tables.

The scenario is as follows; a user can have several roles in several distinct modules, for example: the user John can be administrator in the sales module and operator in the purchasing module, manager in the user control module and so on, in addition to being able to play more than one role in the same module. The tables are: User; Paper; Module and Performance, where only the primary key and the foreign keys of the other tables are used according to the image below: inserir a descrição da imagem aqui

These are my models.

User

    lass Usuario extends Model{
    use Notifiable;
    protected $table='usuario';
    public $timestamps = false;
    protected $fillable = ['foto', 'nome', 'email', 'senha'];
    use SoftDeletes;

    protected $hidden = [
        'password', 'remember_token',
    ];
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function atuacoes()
    {
        return $this->hasMany('Modules\controleUsuario\Entities\Atuacao');
    }

}

Paper:

    class Papel extends Model{
    use SoftDeletes;
    protected $table='papel';
    protected $fillable = ['nome','descricao','created_at','deleted_at'];
    public $timestamps=false;
    protected $dates = ['data'=> 'm-d-Y'];

    public function atuacoes()
    {
        return $this->hasMany('Modules\controleUsuario\Entities\Atuacao');
    }


}

Module:

    class Modulo extends Model{
    use SoftDeletes;
    protected $table='modulo';
    protected $fillable = ['nome', 'icone'];
    public $timestamps=false;

    public function atuacoes()
    {
        return $this->hasMany('Modules\controleUsuario\Entities\Atuacao');
    }
}
e por fim **Atuacao**<br><br>

    class Atuacao extends Model{
    use SoftDeletes;
    protected $table='atuacao';
    protected $timestamps=false;

    public function usuario()
    {
        return $this->belongsTo('Modules\controleUsuario\Entities\Usuario');
    }

    public function papel()
    {
        return $this->belongsTo('Modules\controleUsuario\Entities\Papel');
    }

    public function modulo()
    {
        return $this->belongsTo('Modules\controleUsuario\Entities\Modulo');
    }
}

My difficulty is knowing how to associate and display all the roles and their respective module of a user. If anyone can give a light I would be immensely grateful, thanks in advance

  • I would make a class Acting and call 1 to N all that relate, because you can control this data better, N to M is good in the case when you have only the keys, already in your case until primary key has ... !!! Wouldn’t it be better? it also seems that there is something wrong in the relationship entity model, I would check all this. It seems you even did it in your relationships... kkk

  • This model is not being used at the moment, It was just to illustrate, not all fields filled. But msm so thank you for nothing

  • Their doubt should be better explained, since the model (MER) and the relations are practically as illustrated. " Thanks for nothing" sounds like irony, but what wasn’t clear was your question ... It has the documentation that explains everything how to write, change, exclude and include and how to do the relationships.

1 answer

0

You already have the relationships made based on this, just access them.

foreach($usuario->atuacoes as $atuacao)
{
   echo 'Modulo: ' . $atuacao->modulo->nome . '<br />';
   echo 'Papel: ' . $atuacao->papel->nome . '<br />';
   echo '------------------ <br />';
}

Tip: You can take advantage of the functionality of Eager Loading of Laravel to always bring relationships in the queries of the acting model making them more efficient. See the model with this property.

class Atuacao extends Model{
    use SoftDeletes;

    protected $table = 'atuacao';

    protected $timestamps = false;

    /**
     * The relationships that should always be loaded.
     *
     * @var array
     */
    protected $with = ['papel', 'modulo'];


    public function usuario()
    {
        return $this->belongsTo('Modules\controleUsuario\Entities\Usuario');
    }

    public function papel()
    {
        return $this->belongsTo('Modules\controleUsuario\Entities\Papel');
    }

    public function modulo()
    {
        return $this->belongsTo('Modules\controleUsuario\Entities\Modulo');
    }
}

It is worth taking a look also at Has Many Through.

  • Thanks man, I forgot to comment but that’s right. Thank you very much.

Browser other questions tagged

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