Laravel关系6表

时间:2018-12-23 10:27:43

标签: php laravel laravel-5

enter image description here

解决这种关系的最佳实践是什么?因此,基本上每个用户都拥有自己的tracks,而且每个用户都拥有自己的solvedLessonCount

到目前为止,我所做的是我的模型:

class User extends Model {

    public function tracks()
    {
        return $this->belongsToMany(\App\Models\Track::class, 'track_users', 'user_id', 'track_id')->withTimestamps();
    }
}

class Track extends Model {

    public function users()
    {
        return $this->belongsToMany(\App\User::class, 'track_users', 'track_id', 'user_id');
    }

    public function tutorials()
    {
        return $this->hasMany(\App\Models\Tutorial::class);
    }
}

class Tutorial extends Model {

    public function tracks()
    {
        return $this->belongsTo(\App\Models\Track::class);
    }

    public function chapters()
    {
        return $this->hasMany(\App\Models\Chapter::class);
    }
}

class Chapter extends Model {

    public function tutorials()
    {
        return $this->belongsTo(\App\Models\Tutorial::class);
    }
}

这是我期望每个用户的json响应

user_response.json

0 个答案:

没有答案