本文整理汇总了PHP中Illuminate\Support\Collection::keyBy方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::keyBy方法的具体用法?PHP Collection::keyBy怎么用?PHP Collection::keyBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Support\Collection
的用法示例。
在下文中一共展示了Collection::keyBy方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getCollectionFromCart
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');
}
示例2: index
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'));
}
示例3: select
/**
* 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;
}
示例4: testKeyByClosure
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());
}
示例5: checkMultiGetResults
/**
* 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);
}
}
示例6: setProperties
/**
* Set relation properties.
*
* @param BaseCollection|null $properties
*/
public function setProperties(BaseCollection $properties = null)
{
$this->properties = $properties === null ? null : $properties->keyBy('name');
}
示例7: getIds
/**
* @return array
*/
public function getIds()
{
return $this->fields->keyBy(function (FieldInterface $field) {
return $field->getId();
})->keys()->all();
}