public static function updateArrangement(Collection $collection, array $arrangement) { $order = 0; foreach ($arrangement as $parent) { /** @var \CipeMotion\Medialibrary\Entities\Category $category */ $category = $collection->find(array_get($parent, 'id', 0)); $children = array_get($parent, 'children', []); if (!is_null($category)) { $category->order = $order; $category->parent_id = null; $category->save(); if (count($children) > 0) { $childOrder = 0; foreach ($children as $child) { /** @var \CipeMotion\Medialibrary\Entities\Category $childCategory */ $childCategory = $collection->find(array_get($child, 'id', 0)); if (!is_null($childCategory)) { $childCategory->order = $childOrder; $childCategory->parent_id = $category->id; $childCategory->save(); $childOrder++; } } } $order++; } } }
protected function getCounty($municipality_id) { $county_id = substr(str_pad($municipality_id, 4, '0', STR_PAD_LEFT), 0, 2); /* @var County $county */ $county = $this->counties->find($county_id); return $county; }
/** * Retrieve token data * * @param string $tokenName * * @return mixed */ public function __get($tokenName) { $token = $this->tokens->find($tokenName); if (!$token instanceof Token) { return null; } $tokenVal = $token->token_value; return $tokenVal; }
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; }
/** * */ public function save() { if (empty($this->data)) { return; } if ($this->relation instanceof Relations\HasMany && is_array($this->data)) { if (is_numeric($this->data[0])) { $data = new Collection(); foreach ($this->data as $key => $item) { // $this->data[$key] = $this->related->baseModel->find($item); $data->push($this->related->baseModel->find($item)); } } else { $data = new Collection($this->data); } // detach any existing models and only save the selected ones $foreignKey = $this->relation->getPlainForeignKey(); $current = $this->relation->getResults(); if (!$current) { $this->relation->saveMany($data->toArray()); return; } $all = $data->merge($current); foreach ($all as $item) { if ($keep = $data->find($item->getKey())) { $this->relation->save($keep); } else { $item->{$foreignKey} = null; $item->save(); } } } else { if ($this->relation instanceof Relations\BelongsToMany && is_array($this->data)) { if (is_numeric($this->data[0])) { $this->relation->sync($this->data); } } else { /* If we have an id let's grab the model instance, otherwise assume we were given it */ $this->data = is_numeric($this->data) ? $this->related->baseModel->find($this->data) : $this->data; parent::saveRelation($this->relation, $this->data); } } }
/** * {@inheritdoc} */ public function find($id) { $this->boot(); return $this->collection->find($id); }
/** * Build the pivot table data for a particular item. * * @param mixed $key The product's primary key. * @param array $data * @param \Illuminate\Database\Eloquent\Collection $products * * @return array */ protected function buildItemData($key, array $data, Collection $products) { if (!($product = $products->find($key))) { throw new \RuntimeException("ID {$key} not found in products collection"); } $this->setProduct($product); if (!empty($data['purchased_amount'])) { $data['purchased_for'] = $product->in_price; } if (!empty($data['sold_amount'])) { $data['sold_for'] = $product->out_price; } return $data; }
/** * Updates the current user's searched hashtags with the specified hashtag * $terms and returns the number of hashtags updated or -1 on failure. * * @param Collection $hashtags hashtag collection to update * @param array|string $terms hashtag terms to update with * @return integer */ protected function updateUserHashtags(Collection $hashtags, array $terms) { $num_updates = 0; foreach ($terms as $id => $term) { // remove whitespace, convert to lowercase $term = strtolower(preg_replace('/\\s+/', '', $term)); if (!empty(trim($term))) { // verify hashtag term does not already exists $hashtag = $hashtags->first(function ($key, $value) use($id, $term) { return $value->id != $id && $value->term == $term; }); if (isset($hashtag)) { // indicate error has occured $num_updates = -1; \Session::put('error_hashtag_edit_id', $id); \Session::flash('flash_message', 'Hashtag \'' . $term . '\' already exists.'); break; } // update hashtag term if not the same $hashtag = $hashtags->find($id); if (isset($hashtag) && $hashtag->term != $term) { $hashtag->term = $term; $hashtag->save(); $num_updates++; } } } return $num_updates; }