public function getCollectionFromCart($cart) { $collection = new Collection(); if ($cart->ProductCount == 1) { $collection = new Collection([$cart->Items->CartLine]); } elseif ($cart->ProductCount > 1) { $collection = new Collection($cart->Items->CartLine); } return $collection->keyBy('ProductId'); }
public function index($year, $month) { $photos = new Collection(); for ($date = $this->createDate($year, $month); $date->month == $month; $date->addDay()) { $photo = new Photo($date->copy()); if ($photo->exists()) { $photos->push($photo); } } return response()->json($photos->keyBy('index')); }
/** * From collection get the list use for generate select box HTML::select * The final result will be: * <pre> * [id => name] * </pre> * * @param \Illuminate\Support\Collection $collection * * @return array */ function select($collection) { $area = $collection->keyBy('id')->toArray(); foreach ($area as $k => $v) { $area[$k] = $v['name']; } return $area; }
public function testKeyByClosure() { $data = new Collection([['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'], ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR']]); $result = $data->keyBy(function ($item, $key) { return strtolower($key . '-' . $item['firstname'] . $item['lastname']); }); $this->assertEquals(['0-taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'], '1-lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR']], $result->all()); }
/** * Checks the results of a multi GET for errors. * * @param array $results * @param \Illuminate\Support\Collection $collection * @throws \Elodex\Exceptions\MultiGetException */ protected function checkMultiGetResults(array $results, BaseCollection $collection) { $modelDictionary = $collection->keyBy('id')->all(); $failedItems = []; $errors = []; foreach ($results['docs'] as $doc) { $documentId = $doc['_id']; if (isset($doc['error'])) { $failedItems[$documentId] = $modelDictionary[$documentId]; $errors[$documentId] = $doc['error']; continue; } if ($doc['found'] === false) { $failedItems[$documentId] = $modelDictionary[$documentId]; $errors[$documentId] = ['reason' => 'document not found']; } } if (!empty($failedItems)) { throw MultiGetException::createForFailedItems($failedItems, $errors); } }
/** * Set relation properties. * * @param BaseCollection|null $properties */ public function setProperties(BaseCollection $properties = null) { $this->properties = $properties === null ? null : $properties->keyBy('name'); }
/** * @return array */ public function getIds() { return $this->fields->keyBy(function (FieldInterface $field) { return $field->getId(); })->keys()->all(); }