本文整理汇总了PHP中Illuminate\Support\Collection::unique方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::unique方法的具体用法?PHP Collection::unique怎么用?PHP Collection::unique使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Support\Collection
的用法示例。
在下文中一共展示了Collection::unique方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: makeCollection
/**
* 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;
}
示例2: getFromIds
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);
});
}
示例3: summary
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;
}
示例4: visible_bookmarks_count
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();
}
示例5: addUseStatements
/**
* @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;
}
示例6: unique
/**
* 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()));
}
示例7: testUniqueWithCallback
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());
}