Beispiel #1
0
 public function userWords($box = null)
 {
     $groups = Auth::user()->groups;
     $words = new \Illuminate\Database\Eloquent\Collection();
     foreach ($groups as $group) {
         foreach ($group->words as $word) {
             $words->add($word);
         }
     }
     $userWords = Auth::user()->words;
     /* replace all the words, which are already in the list and also in user_word, to get the correct statistic numbers */
     $userWords->each(function ($word) use($words) {
         if ($words->contains($word->id)) {
             $words->find($word->id)->pivot = $word->pivot;
         }
     });
     $words = $this->sortById($words);
     if ($box == null) {
         /* if no box is selected, return all words the user has selected */
         return $words;
     } else {
         $results = new \Illuminate\Database\Eloquent\Collection();
         $words->each(function ($word) use($results, $groups, $box) {
             if ($groups->contains($word->group_id) && $word->box == $box) {
                 $results->add($word);
             }
         });
         return $results;
     }
 }