本文整理汇总了PHP中Illuminate\Support\Collection::first方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::first方法的具体用法?PHP Collection::first怎么用?PHP Collection::first使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Support\Collection
的用法示例。
在下文中一共展示了Collection::first方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getInitial
public function getInitial()
{
$words = new Collection(explode(' ', $this->name));
// if name contains single word, use first N character
if ($words->count() === 1) {
if ($this->name->length() >= $this->length) {
return $this->name->substr(0, $this->length);
}
return (string) $words->first();
}
// otherwise, use initial char from each word
$initials = new Collection();
$words->each(function ($word) use($initials) {
$initials->push(Stringy::create($word)->substr(0, 1));
});
return $initials->slice(0, $this->length)->implode('');
}
示例2: getFirstWeapon
/**
* @return \Pipboy\Contracts\AbstractWeapon
*/
public function getFirstWeapon()
{
if (is_null($this->items)) {
return false;
}
return $this->items->first(function ($i, AbstractItem $item) {
return !(strpos($item->getuid(), 'weapon') === false);
});
}
示例3: multiYear
/**
* @param Collection $entries
*
* @return array
*/
public function multiYear(Collection $entries)
{
// dataset:
$data = ['count' => 0, 'labels' => [], 'datasets' => []];
// get labels from one of the categories (assuming there's at least one):
$first = $entries->first();
$keys = array_keys($first['spent']);
foreach ($keys as $year) {
$data['labels'][] = strval($year);
}
// then, loop all entries and create datasets:
foreach ($entries as $entry) {
$name = $entry['name'];
$spent = $entry['spent'];
$earned = $entry['earned'];
if (array_sum(array_values($spent)) != 0) {
$data['datasets'][] = ['label' => 'Spent in category ' . $name, 'data' => array_values($spent)];
}
if (array_sum(array_values($earned)) != 0) {
$data['datasets'][] = ['label' => 'Earned in category ' . $name, 'data' => array_values($earned)];
}
}
$data['count'] = count($data['datasets']);
return $data;
}
示例4: resolve
/**
* Determines a winner from the list of buzzes accumulated.
*
* Note: This method is destructive, and will clear the list of buzzes on completion.
* @return BuzzerResolution
*/
public function resolve()
{
if ($this->isEmpty()) {
return BuzzerResolution::createFailure();
}
/** @var BuzzReceivedEvent $winner */
$winner = $this->buzzes->reduce(function (BuzzReceivedEvent $carry, BuzzReceivedEvent $event) {
if ($event->getDifference() < $carry->getDifference()) {
return $event;
}
return $carry;
}, $this->buzzes->first());
$resolution = BuzzerResolution::createSuccess($winner->getContestant(), $winner->getDifference());
$this->buzzes = new Collection();
return $resolution;
}
示例5: makeForCollection
public static function makeForCollection(Collection $collection)
{
if ($collection->count() === 0) {
throw new \OutOfRangeException("Requested collection does not contain any items");
}
return static::makeForModel($collection->first());
}
示例6: __construct
/**
* @param Collection $collection
* @param $request
*/
public function __construct(Collection $collection, $request)
{
$this->collection = $collection;
$this->original_collection = $collection;
$this->columns = array_keys($this->serialize((array) $collection->first()));
parent::__construct($request);
}
示例7: __construct
/**
* @param Collection $collection
* @param \Yajra\Datatables\Request $request
*/
public function __construct(Collection $collection, Request $request)
{
$this->request = $request;
$this->collection = $collection;
$this->original_collection = $collection;
$this->columns = array_keys($this->serialize($collection->first()));
}
示例8: collectPermissions
protected function collectPermissions($permission)
{
if (is_string($permission)) {
$permission = app(PermissionContract::class)->whereSlug($permission)->first();
}
if (is_array($permission) or $permission instanceof Collection) {
$permission = new Collection($permission);
if (is_string($permission->first())) {
return app(PermissionContract::class)->whereIn('slug', $permission->toArray())->get();
} elseif ($permission->first() instanceof PermissionContract) {
return $permission;
}
} elseif ($permission instanceof PermissionContract) {
return $permission;
}
return null;
}
示例9: handleDirectory
/**
* @param $properties
* @param $content
*/
private function handleDirectory(Collection $properties, $content)
{
$properties->push($content['filename']);
$this->filesystem->createDir('Api/' . $this->endpoint->first());
$generator = new EndpointGenerator($this->inputInterface, $this->out, $this->dialogHelper);
$endpoint = str_replace("Request/", "", $content['path']);
$generator->generateForEndpoint($endpoint);
}
示例10: single
/**
* @param Collection $schedules
* @return \Filmoteca\Models\Activity
*/
public function single(Collection $schedules)
{
$activity = new Activity();
$firstSchedule = $schedules->first();
$film = $firstSchedule->exhibition->exhibition_film->film;
$auditorium = $firstSchedule->auditorium;
$activity->setId($film->id . $auditorium->id)->setTitle($firstSchedule->exhibition->exhibition_film->title)->setFacility($auditorium)->setPublicType('General')->setContact(self::getContact($firstSchedule))->setSchedules(Formatter::toDateTime($schedules))->setExtraSchedules(Formatter::toExtraTime($schedules))->setPrices(Formatter::toPrices($schedules))->setDiscounts(Formatter::toDiscounts($schedules))->setCommentaries($firstSchedule->exhibition->notes)->setCategory(self::CATEGORY)->setImage($film->image)->setReview($firstSchedule->exhibition->exhibition_film->film->synopsis);
return $activity;
}
示例11: toArray
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
if ($this->channels instanceof Collection) {
// 先拿到第一个子级频道项,并拿到它的父级频道ID
// 开始遍历整个频道栏目,但是跳过第一条记录
// 当然,每当遇到父级频道ID与当前频道ID相同
// 则该为根级频道,一次搜索完成
$first = $this->channels->first();
$cursor = $first->channel_id;
$iterator = $this->channels->getIterator();
while ($iterator->valid() && ($channel = $iterator->current())) {
echo 'prepare' . PHP_EOL;
if ($channel->channel_id == $cursor || $channel->flag === 1) {
$iterator->next();
continue;
}
echo 'start' . PHP_EOL;
var_dump($first->channel_id, $first->channel_parent);
// 当一次搜索完成时,重新搜索数据集
if (isset($_parent) && $_parent->channel_id === $_parent->channel_parent) {
echo 'rewind' . PHP_EOL;
$iterator->rewind();
continue;
}
if ($channel->channel_id === $first->channel_parent) {
$_parent = $channel;
echo 'merge' . PHP_EOL;
var_dump($_parent->channel_id);
$this->{$_parent->channel_type}[$_parent->channel_name] = array_merge($this->{$_parent->channel_type}, ['data' => $first, 'idx' => $cursor, 'to' => $_parent->channel_id]);
echo 'to wind' . PHP_EOL;
var_dump($this->{$_parent->channel_type});
// 因为已经设置过子父级关系,则当前数据项设置为1
$cursor = $_parent->channel_id;
$first = $_parent;
$channel->flag = 1;
}
echo 'complete' . PHP_EOL;
$iterator->next();
}
}
return array_merge(['default' => $this->default], ['mega' => $this->mega]);
}
示例12: 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;
}
示例13: getUserTrends
public function getUserTrends(Collection $rounds)
{
$first = $rounds->first();
$last = $rounds->last();
$totalRounds = $rounds->count();
$strokes = ($last->totalStrokes() - $first->totalStrokes()) / $totalRounds;
$putts = ($last->totalPutts() - $first->totalPutts()) / $totalRounds;
$strokesPar3 = ($last->totalStrokesPar(3) - $first->totalStrokesPar(3)) / $totalRounds;
$strokesPar4 = ($last->totalStrokesPar(4) - $first->totalStrokesPar(4)) / $totalRounds;
$strokesPar5 = ($last->totalStrokesPar(5) - $first->totalStrokesPar(5)) / $totalRounds;
return compact('strokes', 'putts', 'strokesPar3', 'strokesPar4', 'strokesPar5');
}
示例14: getMeta
/**
* Gets meta data
*
* @return Illuminate\Support\Collection
*/
public function getMeta($key, $getObj = false)
{
$meta = $this->meta()->where('key', $key)->get();
if ($getObj) {
$collection = $meta;
} else {
$collection = new Collection();
foreach ($meta as $m) {
$collection->put($m->id, $m->value);
}
}
return $collection->count() <= 1 ? $collection->first() : $collection;
}
示例15: update
/**
* @param \Illuminate\Support\Collection $collection
* @param array $attributes
*
* @return bool|int|\WP_Error
*/
public static function update(Collection $collection, $attributes = [])
{
$user = $collection->first();
$attr = ['ID' => $user->ID];
$self = new static();
$attributes = $self->prepareFields($attributes, 'updating');
$user_id = wp_update_user(array_merge($attributes, $attr));
if (is_wp_error($user_id)) {
return $user_id;
}
$self->addOrUpdateMetafields($user->ID, $attributes);
return true;
}