當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Collection::first方法代碼示例

本文整理匯總了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('');
 }
開發者ID:explore-laravel,項目名稱:avatar,代碼行數:17,代碼來源:InitialGenerator.php

示例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);
     });
 }
開發者ID:howlowck,項目名稱:pipboy-unit,代碼行數:12,代碼來源:User.php

示例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;
 }
開發者ID:zjean,項目名稱:firefly-iii,代碼行數:30,代碼來源:ChartJsCategoryChartGenerator.php

示例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;
 }
開發者ID:mattsches,項目名稱:Jeopardy,代碼行數:22,代碼來源:Resolver.php

示例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());
 }
開發者ID:efrane,項目名稱:transfugio,代碼行數:7,代碼來源:TransformerFactory.php

示例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);
 }
開發者ID:Kirylka,項目名稱:Scaffold,代碼行數:11,代碼來源:CollectionEngine.php

示例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()));
 }
開發者ID:marcha,項目名稱:laravel-datatables,代碼行數:11,代碼來源:CollectionEngine.php

示例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;
 }
開發者ID:znck,項目名稱:trust,代碼行數:17,代碼來源:Role.php

示例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);
 }
開發者ID:ValentinGot,項目名稱:trakt-api-wrapper,代碼行數:12,代碼來源:EndpointGenerator.php

示例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;
 }
開發者ID:filmoteca,項目名稱:filmoteca,代碼行數:13,代碼來源:ActivityFactory.php

示例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]);
 }
開發者ID:AInotamm,項目名稱:Haku.moe,代碼行數:47,代碼來源:Channel.php

示例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;
 }
開發者ID:Coperable,項目名稱:slam_webapp,代碼行數:54,代碼來源:RegionController.php

示例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');
 }
開發者ID:robeasdon,項目名稱:golf-stat-tracker,代碼行數:12,代碼來源:UserRepository.php

示例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;
 }
開發者ID:kee-work,項目名稱:scubaclick-meta,代碼行數:18,代碼來源:MetaTrait.php

示例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;
 }
開發者ID:bruno-barros,項目名稱:w.eloquent-helpers,代碼行數:19,代碼來源:User.php


注:本文中的Illuminate\Support\Collection::first方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。