keyBy() public method

Key an associative array by a field or using a callback.
public keyBy ( callable | string $keyBy ) : static
$keyBy callable | string
return static
 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');
 }
Example #2
0
 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;
 }
Example #4
0
 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());
 }
Example #5
0
 /**
  * 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);
     }
 }
Example #6
0
 /**
  * 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();
 }