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:
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
– novic
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
– Diego Magno
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.
– novic