Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Table: Users
- id - int (auto increment)
- username - varchar
- Table: Challenges
- id - int (auto increment)
- name - varchar
- Table User_challenge_links
- id - int (auto increment)
- user_id - int
- challenge_sub_categories_id - int
- Table Challenge_sub_categories
- id - int (auto increment)
- category_id -
- sub_category_id -
- challenge_id -
- class User extends Eloquent
- {
- protected $table = "users";
- public function challenges() {
- // get user-> challenges
- }
- }
- Users
- id name
- 1 "Sjaak"
- 2 "Henk"
- id category_id sub_category_id challenge_id
- 1 1 1 1
- 2 2 1 1
- 3 1 2 2
- 4 2 1 3
- 5 2 2 2
- id user_id Challenge_sub_categories_id
- 1 1 1
- 2 1 3
- 3 2 2
- 4 2 3
- 5 2 4
- category - House
- sub_category - Cleaning
- Challenge - getting special soap
- category - Car
- sub_category - Cleaning
- Challenge - getting special soap
- category - Showering
- Challenge - getting special soap
- users
- id
- username
- challenge_user
- user_id
- challenge_id
- challenges
- id
- name
- topic_id
- category_id
- topics
- id
- name
- categories
- id
- name
- class User extends Eloquent {
- public function challenges() {
- return $this->belongsToMany('Challenge');
- }
- }
- class Challenge extends Eloquent {
- public function users() {
- return $this->belongsToMany('User');
- }
- public function topic() {
- return $this->belongsTo('Topic');
- }
- public function category() {
- return $this->belongsTo('Category');
- }
- }
- class Topic extends Eloquent {
- public function challenges() {
- return $this->hasMany('Challenge');
- }
- }
- class Category extends Eloquent {
- public function challenges() {
- return $this->hasMany('Challenge');
- }
- }
- // Collection of all Challenges by Topic name
- Topic::with('challenges')->whereName($topic_name)->first()->challenges;
- // Collection of all Challenges by Category name
- Category::with('challenges')->whereName($category_name)->first()->challenges;
- // Collection of all Users by Challenge id
- Challenge::with('users')->find($challenge_id)->users;
- // Collection of Users with atleast 2 Challenges
- User::has('challenges', '>', 1)->get();
- // Attach Challenge to User
- $user = User::find($id);
- $user->challenges()->attach($challenge_id);
- // Assign a Topic to a Challenge
- $challenge = Challenge::find($challenge_id);
- $topic = Topic::find($topic_id);
- $challenge->topic()->associate($topic);
- $challenge->save();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement