dimanche 23 décembre 2018

Laravel relationship 6 tables

enter image description here

what is the best practice to solve this relationship ? so basically every single user have their own tracks and also every single user have their solvedLessonCount

what I've done so far this is my Model:

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);
    }
}

this what I expected json response for every user

user_response.json



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire