I am trying to extend a User model with associated Skills and Levels. Where all users will have different skill levels for a variety of skills. Im trying to figure out the most efficient way to setup the relationships to accomplish this.
Im currently setting Skill to Level as a many to many relationship. User to skills_has_levels is one to many but of no current use.
There are many skills a user can have and each skill can have a different level from no experience to expert.
[Tables]
users
id, username, email, pwd, token, timestamps
skills
id, name, order
levels
id, name, value
skills_has_levels
id, skills_id, levels_id, users_id
In the Skill model:
$this->belongsToMany('App\Level’)
->withPivot(‘users_id’);
To list all users, their skills, and levels :
$users = Users::all()->toArray();
foreach($users as $user)
{
echo "User ID: ".$user->id;
$skills = Skill::where(‘users_id’, ’=‘, $user->id)->get()->toArray();
print_r($skills);
echo "\n\n\n";
}
Ultimately, I would want the User model to return a skills array of all the associated user skills: skill name, skill order, level name and level value.
What I currently do works but i feel it could be done better.
whats the best way to structure the relationships for the User model to connect to skills names and the associated levels values?
Is it possible to related the Skill/Level directly to the User model? So i only have to use the User model to access all of the associated skill and levels.
Thanks in advance.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire