当前位置: 首页>>代码示例>>PHP>>正文


PHP Collection::each方法代码示例

本文整理汇总了PHP中Illuminate\Database\Eloquent\Collection::each方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::each方法的具体用法?PHP Collection::each怎么用?PHP Collection::each使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Illuminate\Database\Eloquent\Collection的用法示例。


在下文中一共展示了Collection::each方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: delete

 /**
  * Remove the given model from the index.
  *
  * @param  Collection  $models
  * @return void
  */
 public function delete($models)
 {
     $params['body'] = [];
     $models->each(function ($model) use(&$params) {
         $params['body'][] = ['delete' => ['_id' => $model->getKey(), '_index' => $this->index, '_type' => $model->searchableAs()]];
     });
     $this->elastic->bulk($params);
 }
开发者ID:ericktamayo,项目名称:laravel-scout-elastic,代码行数:14,代码来源:ElasticsearchEngine.php

示例2: sortByLikes

 private function sortByLikes(Collection $collection, Collection $orderArray)
 {
     $sorted = new Collection();
     $orderArray->each(function ($orderElem) use($collection, $sorted) {
         if ($collection->contains($orderElem->likeable_id)) {
             $sorted->push($collection->find($orderElem->likeable_id));
         }
     });
     return $sorted;
 }
开发者ID:SenhorBardell,项目名称:yol,代码行数:10,代码来源:FavoritesController.php

示例3: getCurrentStatus

 /**
  * Get the current status of the xml files according to the organization's segmentation/publishing type.
  * @param $currentFiles
  * @return array
  */
 public function getCurrentStatus(Collection $currentFiles)
 {
     $currentStatus = [];
     if (!$currentFiles->isEmpty()) {
         $currentFiles->each(function ($file) use(&$currentStatus) {
             $currentStatus[$file->filename]['included_activities'] = $file->extractActivityId();
             $currentStatus[$file->filename]['published_status'] = $file->published_to_register;
         });
     }
     return $currentStatus;
 }
开发者ID:younginnovations,项目名称:aidstream,代码行数:16,代码来源:OrganizationDataProvider.php

示例4: getWithProducts

 /**
  * Returns all products contained in the orders associated with a freeproduct
  * @return freeproducts collection of all the products contained in the orders associated with freeproduct, but defined as a property of the model
  */
 public static function getWithProducts(Collection $items)
 {
     $freeproducts = $items->each(function ($item, $key) {
         $list_products_orders = Collection::make();
         foreach ($item->orders as $order) {
             $list_products_orders = $list_products_orders->merge($order->products);
         }
         $item->products = $list_products_orders;
     });
     return $freeproducts;
 }
开发者ID:masterpowers,项目名称:antVel,代码行数:15,代码来源:FreeProduct.php

示例5: grantInheritanceTo

 public function grantInheritanceTo(\App\Role $successor, \Illuminate\Database\Eloquent\Collection $predecessors)
 {
     $predecessors->each(function ($predecessor) use($successor) {
         $permissions = $predecessor->permissions()->get();
         if (empty($permissions)) {
             dump('Tried to grant [' . $successor->name . '] The permissions equal to those of [' . $predecessor->name . '] but the latter seems to have none granted.');
         } else {
             foreach ($predecessor->permissions()->get() as $permission) {
                 if ($permission instanceof \App\Permission) {
                     dump('[' . $successor->name . '] Inherited [' . $permission->name . '] Thru [' . $predecessor->name . ']');
                     $successor->givePermissionTo($permission);
                 } else {
                     $permission = \App\Permission::where(['name' => $permission])->first();
                     dump('[' . $successor->name . '] Inherited [' . $permission->name . '] Thru [' . $predecessor->name . ']');
                     $successor->givePermissionTo($permission);
                 }
             }
         }
     });
 }
开发者ID:sasin91,项目名称:laravel-5-roles-and-permissions-demo,代码行数:20,代码来源:RoleSeeder.php

示例6: setCountedFields

 /**
  * @param Collection $collection
  * @return Collection
  */
 public function setCountedFields(Collection $collection)
 {
     $id = [];
     $collection->each(function ($item, $key) use(&$id) {
         $id[] = $item->id;
     });
     foreach ($this->relations as $relation => $options) {
         $tmp = $this->getRelationRecordCount($relation, $relation)->whereIn('main.id', $id)->get()->all();
         $tmp2 = [];
         foreach ($options['fields'] as $field) {
             foreach ($tmp as $record) {
                 $tmp2[$record->id] = $record->{$field};
             }
             $collection = $collection->each(function ($item, $key) use($field, $tmp2) {
                 $item[$field] = !empty($tmp2[$item->id]) ? $tmp2[$item->id] : 0;
             });
         }
     }
     return $collection;
 }
开发者ID:B1naryStudio,项目名称:asciit,代码行数:24,代码来源:Repository.php

示例7: getSegmentationsAttribute

 public function getSegmentationsAttribute()
 {
     $segmentations = new Collection(DB::select('
   SELECT IS_F.Id AS FileId, IS_S.SegmentationType AS SegmentationType, IS_F.FileName AS FileName, IS_F.Extension AS Extension
   FROM ItemSet_Segmentation IS_S
    JOIN ItemSet_Patient IS_P ON IS_S.Patient_Id=IS_P.Id
    JOIN ItemSet_VtkFile IS_V ON IS_S.Id=IS_V.Segmentation_Id
    JOIN ItemSet_File IS_F ON IS_F.Id=IS_V.Id
   WHERE IS_P.Id=:PatientId AND IS_S.State=3
 ', ['PatientId' => $this->Patient_Id]));
     if ($this->Parent_Id && $this->Parent->hasSimulatedLesion) {
         $segmentations[] = $this->Parent->SimulatedLesion;
     }
     $segmentations->each(function ($s) {
         $s->Name = SegmentationTypeEnum::get($s->SegmentationType);
     });
     return $segmentations;
 }
开发者ID:philtweir,项目名称:glossia-scratch-test-site,代码行数:18,代码来源:Simulation.php

示例8: collectRelations

 /**
  * Collect relations from a collections. This will group the
  * comment relations to new collection. For example:
  * person1: {jobs: []}, person2: {jobs:[]}
  * ==> person1, person2 :: {jobs: []}
  *
  * @param \Illuminate\Database\Eloquent\Collection $instance
  * @return array
  */
 private function collectRelations(Collection $instance)
 {
     $sub_result = array();
     // This is the magic function where we process all
     // relationships. If the relatinship is a model, change it to
     // collection. If more than one model-relationship exists,
     // they are all added to collection and filtered.
     $instance->each(function ($item) use(&$sub_result) {
         $serializables = $item->getSideLoads();
         foreach ($serializables as $serializable) {
             $key = $serializable;
             if (!in_array($key, array('people', 'men', 'women', 'children'))) {
                 $key = str_plural($key);
             }
             $item_relation = $item->{$serializable};
             if (!$this->isEmptyOrNull($item_relation)) {
                 if (!$this->isCollection($item_relation)) {
                     $rel = new Collection();
                     $item_relation = $rel->add($item_relation);
                 } else {
                     $item_relation = $item_relation->unique();
                 }
                 if (array_key_exists($key, $sub_result)) {
                     $sub_result[$key] = $sub_result[$key]->merge($item_relation)->unique();
                 } else {
                     $sub_result[$key] = $item_relation;
                 }
             }
         }
     });
     return $sub_result;
 }
开发者ID:andizzle,项目名称:rest-framework,代码行数:41,代码来源:JSONSerializer.php

示例9: getGanttHierarchy

 public static function getGanttHierarchy(Collection $tasks)
 {
     $map = [];
     $tasks->each(function ($task) use(&$map) {
         $task = $task->toArray();
         $task['slaves'] = [];
         $task['from'] = $task['from'] ? Carbon::createFromFormat('Y-m-d h:i:s', $task['from'])->format('Y-m-d') : null;
         $task['to'] = $task['to'] ? Carbon::createFromFormat('Y-m-d h:i:s', $task['to'])->format('Y-m-d') : null;
         $map[$task['id']] = $task;
     });
     $tasks->each(function ($task) use(&$map) {
         if (!$task->master_id) {
             return;
         }
         $map[$task->master_id]['slaves'][] =& $map[$task->id];
     });
     return (new \Illuminate\Support\Collection($map))->filter(function ($task) {
         return $task['master_id'] == null;
     })->toArray();
 }
开发者ID:titus-toia,项目名称:treabar,代码行数:20,代码来源:Task.php

示例10: createWith

 public static function createWith(array $attributes, array $relations, array $options = [])
 {
     // we need to fire model events on all the models that are involved with our operaiton,
     // including the ones from the relations, starting with this model.
     $me = new static();
     $models = [$me];
     $query = static::query();
     $grammar = $query->getQuery()->getGrammar();
     // add parent model's mutation constraints
     $label = $grammar->modelAsNode($me->getDefaultNodeLabel());
     $query->addManyMutation($label, $me);
     // setup relations
     foreach ($relations as $relation => $values) {
         $related = $me->{$relation}()->getRelated();
         // if the relation holds the attributes directly instead of an array
         // of attributes, we transform it into an array of attributes.
         if (!is_array($values) || Helpers::isAssocArray($values)) {
             $values = [$values];
         }
         // create instances with the related attributes so that we fire model
         // events on each of them.
         foreach ($values as $relatedModel) {
             // one may pass in either instances or arrays of attributes, when we get
             // attributes we will dynamically fill a new model instance of the related model.
             if (is_array($relatedModel)) {
                 $relatedModel = $related->fill($relatedModel);
             }
             $models[] = $relatedModel;
             $query->addManyMutation($relation, $related);
         }
     }
     // fire 'creating' and 'saving' events on all models.
     foreach ($models as $model) {
         // we will fire model events on actual models, however attached models using IDs will not be considered.
         if ($model instanceof Model) {
             if ($model->fireModelEvent('creating') === false) {
                 return false;
             }
             if ($model->fireModelEvent('saving') === false) {
                 return false;
             }
         }
     }
     // run the query and create the records.
     $result = $query->createWith($attributes, $relations);
     // take the parent model that was created out of the results array based on
     // this model's label.
     $created = reset($result[$label]);
     // fire 'saved' and 'created' events on parent model.
     $created->finishSave($options);
     $created->fireModelEvent('created', false);
     // set related models as relations on the parent model.
     foreach ($relations as $method => $values) {
         $relation = $created->{$method}();
         // is this a one-to-one relation ? If so then we add the model directly,
         // otherwise we create a collection of the loaded models.
         $related = new Collection($result[$method]);
         // fire model events 'created' and 'saved' on related models.
         $related->each(function ($model) use($options) {
             $model->finishSave($options);
             $model->fireModelEvent('created', false);
         });
         // when the relation is 'One' instead of 'Many' we will only return the retrieved instance
         // instead of colletion.
         if ($relation instanceof OneRelation || $relation instanceof HasOne || $relation instanceof BelongsTo) {
             $related = $related->first();
         }
         $created->setRelation($method, $related);
     }
     return $created;
 }
开发者ID:noodle-learns-programming,项目名称:jobsearch,代码行数:71,代码来源:Model.php

示例11: savePermissions

 /**
  * Save permissions per action
  *
  * @param Collection $roles
  * @param Action $action
  */
 protected function savePermissions(Collection $roles, Action $action)
 {
     $roles->each(function ($role) use($action) {
         $permission = new Permission();
         $permission->role()->associate($role);
         $permission->action()->associate($action);
         $permission->save();
     });
     $permission = new Permission();
     $permission->action()->associate($action);
     $permission->save();
 }
开发者ID:Denniskevin,项目名称:Laravel5Starter,代码行数:18,代码来源:AclClear.php


注:本文中的Illuminate\Database\Eloquent\Collection::each方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。