Personally I would go with Many-to-Many relations, the most versatile. I don't play golf, so don't really know a hierarchy of courses, tables, but I bet many users belong to many results and player tables etc.
About simple query between pivots, take a look at whereHas, that should be powerful enough.
http://laravel.com/docs/eloquent#relationships
http://belardesign.com/2014/02/13/relation-pivot-based-queries-with-wherehas/ Yeah, self advertisment at it's finest D.
I've modified the database a bit so i can use many-to-many relations. Now the tables look like this. Rounds table:
id
user_id
course_id
played_at
created_at
updated_at
Results table:
id
round_id
user_id
hole
strokes
created_at
updated_at
Pivot table
round_id
user_id
result_id
Each user in the round will get a row in the pivot table with the result_id being auto incrementing. That id will then be the value of results table column id for all the 18 rows that each user in the round creates each round.
When i try to do
return Round::with('players.results')->find(3);
I get the round and players relationships correctly but instead of grabbing the results for each user it grabs the score from the first round that were reported for every user. So if there are three users with for example an id of 1, 2 and 3 each user in the round i queried after get the first overall result from both user 1,2 and 3.
Hi there, im still learning Laravel and im sure this is not the best way to do it, but i think it will work.
Rounds table:
id
played_at
(...)
Players table:
id
name
age
(...)
Results table:
id
round_id
user_id
hole
strokes
(...)
Results::where('round_id', $round)->where('user_id', $user)->get();
I think the proper way to do it is the Results table be the pivot table between Players and Rounds.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community