Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. Table: Users
  2. id - int (auto increment)
  3. username - varchar
  4.  
  5. Table: Challenges
  6. id - int (auto increment)
  7. name - varchar
  8.  
  9. Table User_challenge_links
  10. id - int (auto increment)
  11. user_id - int
  12. challenge_sub_categories_id - int
  13.  
  14. Table Challenge_sub_categories
  15. id - int (auto increment)
  16. category_id -
  17. sub_category_id -
  18. challenge_id -
  19.  
  20. class User extends Eloquent
  21. {
  22. protected $table = "users";
  23.  
  24. public function challenges() {
  25. // get user-> challenges
  26. }
  27. }
  28.  
  29. Users
  30. id name
  31. 1 "Sjaak"
  32. 2 "Henk"
  33.  
  34. id category_id sub_category_id challenge_id
  35. 1 1 1 1
  36. 2 2 1 1
  37. 3 1 2 2
  38. 4 2 1 3
  39. 5 2 2 2
  40.  
  41. id user_id Challenge_sub_categories_id
  42. 1 1 1
  43. 2 1 3
  44. 3 2 2
  45. 4 2 3
  46. 5 2 4
  47.  
  48. category - House
  49. sub_category - Cleaning
  50. Challenge - getting special soap
  51.  
  52. category - Car
  53. sub_category - Cleaning
  54. Challenge - getting special soap
  55.  
  56. category - Showering
  57. Challenge - getting special soap
  58.  
  59. users
  60. id
  61. username
  62.  
  63. challenge_user
  64. user_id
  65. challenge_id
  66.  
  67. challenges
  68. id
  69. name
  70. topic_id
  71. category_id
  72.  
  73. topics
  74. id
  75. name
  76.  
  77. categories
  78. id
  79. name
  80.  
  81. class User extends Eloquent {
  82. public function challenges() {
  83. return $this->belongsToMany('Challenge');
  84. }
  85. }
  86.  
  87. class Challenge extends Eloquent {
  88. public function users() {
  89. return $this->belongsToMany('User');
  90. }
  91. public function topic() {
  92. return $this->belongsTo('Topic');
  93. }
  94. public function category() {
  95. return $this->belongsTo('Category');
  96. }
  97. }
  98.  
  99. class Topic extends Eloquent {
  100. public function challenges() {
  101. return $this->hasMany('Challenge');
  102. }
  103. }
  104.  
  105. class Category extends Eloquent {
  106. public function challenges() {
  107. return $this->hasMany('Challenge');
  108. }
  109. }
  110.  
  111. // Collection of all Challenges by Topic name
  112. Topic::with('challenges')->whereName($topic_name)->first()->challenges;
  113.  
  114. // Collection of all Challenges by Category name
  115. Category::with('challenges')->whereName($category_name)->first()->challenges;
  116.  
  117. // Collection of all Users by Challenge id
  118. Challenge::with('users')->find($challenge_id)->users;
  119.  
  120. // Collection of Users with atleast 2 Challenges
  121. User::has('challenges', '>', 1)->get();
  122.  
  123. // Attach Challenge to User
  124. $user = User::find($id);
  125. $user->challenges()->attach($challenge_id);
  126.  
  127. // Assign a Topic to a Challenge
  128. $challenge = Challenge::find($challenge_id);
  129. $topic = Topic::find($topic_id);
  130.  
  131. $challenge->topic()->associate($topic);
  132. $challenge->save();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement