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 (ie. laravel, angular, bootstrap, etc)
id, name, order
levels (ie. none, beginner, intermediate, expert)
id, name, value
skills_has_levels
id, skills_id, levels_id, users_id
We have three models:
1) User (has many skills with levels)
2) Skill (has many levels)
3) Level (has many skills)
In the Skill model: (to have the users_id available: unused)
$this->belongsToMany('App\Level’)
->withPivot(‘users_id’);
To list all users, their skills, and levels :
$users = User::all()->toArray();
foreach($users as $user)
{
echo "User ID: ".$user->id;
$skills = Skill::where(‘users_id’, ’=‘, $user->id)->get()->toArray();
print_r($skills);
echo "
";
}
Ultimately, I would want the User model to return a skills/levels array of all the associated user skills & their levels: 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.
Or are my relationships wrong? Could change the models to this relationship:
A User has many Skill
Skill has many skill types
Skill has many skill levels
Thanks in advance.