I have 4 tables:
Tournament
id | name
-----------------------------------
1 | National Tournament 2014
-----------------------------------
2 | National Tournament 2015
-----------------------------------
3 | Tournament Test
-----------------------------------
Category
id | name
-----------------------------------
1 | Men >80kg
-----------------------------------
2 | Women >80kg
-----------------------------------
3 | Team Men >80kg
-----------------------------------
Category_Tournament (List all categories available in a tournament)
id | category_id | tournament_id
---------------------------
1 | 1 | 2
---------------------------
2 | 2 | 2
---------------------------
3 | 3 | 2
---------------------------
4 | 4 | 2
---------------------------
Category_Tournament_User (categories registered by user)
id | category_tournament_id | user_id
-----------------------------------------------
1 | 1 | 201
-----------------------------------------------
2 | 3 | 202
-----------------------------------------------
I have 4 models ( Tournament, Category, CategoryTournament, CategoryTournamentUser)
I have no clue how to use eloquent relationship to link Tournament and Category_Tournament_User
I would like get the all the categories that has registered a user for a given tournament
Another example is to get all the users registered to a tournament.
In Model Tournament, I have
class Tournament extends Model{
public function competitors()
{
return $this->belongsToMany('App\User', 'category_tournament', 'category_tournament_id', 'user_id' )
->withPivot('confirmed')
->withTimestamps();
}
I would like to get all competitors in a tournament.
It won't work, because it is relating tournament_id with category_tournament_id
I could do it in SQL, or in Model based queries, but what I can't understand is how to define and use ManyToMany Relationships between Tournament and Category_Tournament_User for example.
This is a key for such an elegant code, and I can't find the way to implement it, !
Please help me to find my way!!!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire