unique() public method

Return only unique items from the collection array.
public unique ( string | callable | null $key = null, boolean $strict = false ) : static
$key string | callable | null
$strict boolean
return static
 /**
  * Make collection of value objects.
  *
  * @param array $items
  *
  * @return Collection
  */
 protected function makeCollection(array $items)
 {
     $objects = new Collection();
     foreach ($items as $item) {
         $objects->push(new $this->valueObject($item));
     }
     return $this->unique ? $objects->unique() : $objects;
 }
Example #2
0
 public function getFromIds(Collection $idCollection, $method, $force = false)
 {
     if ($idCollection->isEmpty()) {
         return $idCollection;
     }
     return $idCollection->unique()->map(function ($item, $key) use($force, $method) {
         return call_user_func([$this, $method], $item, $force);
     });
 }
 public function summary(Request $request, $regionId)
 {
     $region = Region::find($regionId);
     $user = false;
     $token = $request->header('Authorization');
     if ($token) {
         if (isset($token[1])) {
             $token = explode(' ', $request->header('Authorization'))[1];
             $payload = (array) JWT::decode($token, Config::get('app.token_secret'), array('HS256'));
             $user = User::find($payload['sub']);
         }
     }
     $participants = new Collection();
     $past_competitions = new Collection();
     $next_competitions = new Collection();
     $next_competition = array();
     $competitions = array();
     if ($regionId == 1) {
         $competitions = Competition::all();
         $videos = DB::table('medias')->where('region_id', '<>', $region->id)->get();
         $region->competitions = $competitions;
     } else {
         $competitions = $region->competitions;
         $videos = DB::table('medias')->where('region_id', '=', $region->id)->get();
     }
     $competitions->each(function ($competition) use($past_competitions, $next_competitions, $participants, $user) {
         $competition->users->each(function ($participant) use($participants, $competition, $user) {
             if ($user && $user->id == $participant->id) {
                 $competition->already_participating = true;
             }
             $participant->medias;
             $participant->competitions;
             $participants->push($participant);
         });
         $competition->location;
         $competition->videos;
         if (Carbon::now()->gte($competition->event_date)) {
             $competition->past = true;
             $past_competitions->push($competition);
         } else {
             $competition->past = false;
             $next_competitions->push($competition);
         }
     });
     $region->next_competition = $next_competitions->first();
     $region->next_competitions = $next_competitions;
     $region->past_competitions = $past_competitions;
     $region->videos = $videos;
     $region->videos_count = count($videos);
     $region->competitions_count = count($competitions);
     $region->participants = $participants->unique();
     $region->participants_count = count($region->participants);
     return $region;
 }
Example #4
0
 public function visible_bookmarks_count()
 {
     $this_tag_name = $this->name;
     $bookmarks = new Collection();
     $all_bookmarks = Bookmark::where('user_id', '=', Auth::user()->id)->orWhere('private', '=', false)->get();
     //iterate through all of the potential bookmarks
     foreach ($all_bookmarks as $bookmark) {
         $tags = $bookmark->tags()->get();
         foreach ($tags as $tag) {
             if ($tag->name == $this_tag_name) {
                 //user is allowed to see this one so add to collection
                 $bookmarks->push($bookmark);
                 break;
             }
         }
     }
     //done
     return $bookmarks->unique('id')->count();
 }
 /**
  * @return $this
  */
 private function addUseStatements()
 {
     if ($this->endpoint->count() > 1) {
         $this->uses->push(new ReflectionClass(Endpoint::class));
     }
     $unique = $this->uses->unique();
     $aliases = new Collection();
     $unique->each(function ($useStatement) use($aliases) {
         /** @var ReflectionClass $useStatement */
         $parent = $useStatement->getParentClass();
         if ($parent !== false && $parent->getName() === AbstractRequest::class) {
             $aliases->push($useStatement->getName() . " as " . $useStatement->getShortName() . "Request");
         } else {
             $aliases->push($useStatement->getName());
         }
     });
     if ($aliases->count() > 0) {
         $uses = $aliases->implode(";\nuse ");
         $this->out->writeln("Adding use statements to template");
         return $this->writeInTemplate("use_statements", "use " . $uses . ";");
     }
     return $this;
 }
 /**
  * Return only unique items from the collection.
  *
  * @param  string|callable|null  $key
  * @return static
  */
 public function unique($key = null)
 {
     if (!is_null($key)) {
         return parent::unique($key);
     }
     return new static(array_values($this->getDictionary()));
 }
Example #7
0
 public function testUniqueWithCallback()
 {
     $c = new Collection([1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'], 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'], 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'], 4 => ['id' => 4, 'first' => 'Abigail', 'last' => 'Otwell'], 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'], 6 => ['id' => 6, 'first' => 'Taylor', 'last' => 'Swift']]);
     $this->assertEquals([1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'], 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell']], $c->unique('first')->all());
     $this->assertEquals([1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'], 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'], 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift']], $c->unique(function ($item) {
         return $item['first'] . $item['last'];
     })->all());
 }