dongliulu1122 2019-03-25 16:33
浏览 73
已采纳

如何使用其他多对多关系模型扩展用户模型技能/级别?

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.

  • 写回答

2条回答 默认 最新

  • dorisdong0514 2019-03-26 14:12
    关注

    Your database makes the work harder i guess You can use dynamic properties if you want to make it available in usermodel

    Models User <- -(Many to many) -> Levels <- -(Many to many)

    protected $appends = ['skills_levels'];
    
    public function getSkillsLevelsAttribute()
    {
    
        $skills = // query to get all skills ->pluck('skill','id');
    
        $this->load('levels.skills')
        $levels = $this->levels->groupBy('skills.skill_id')->toarray();
        $output = [];
    
        foreach ($skills as $id => $skill) {
            $output[$skill] = [];
            if (isset($levels[$id])) {
                $output[$skill] = $levels[$id] // all levels user have for this skill
            }
        }
        return $output
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度