/** * 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); }
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; }
/** * 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; }
/** * 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; }
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); } } } }); }
/** * @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; }
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; }
/** * 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; }
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(); }
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; }
/** * 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(); }