解决这种关系的最佳实践是什么?因此,基本上每个用户都拥有自己的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响应