当前位置: 首页>>代码示例>>PHP>>正文


PHP Collection::toArray方法代码示例

本文整理汇总了PHP中Illuminate\Database\Eloquent\Collection::toArray方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::toArray方法的具体用法?PHP Collection::toArray怎么用?PHP Collection::toArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Illuminate\Database\Eloquent\Collection的用法示例。


在下文中一共展示了Collection::toArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: insertAllRow

 protected function insertAllRow($sheet)
 {
     $sheet->each(function ($row) {
         $data = $this->parseFilm($row);
         $data['genre_id'] = $this->findGenreId($row['genero'], $data['title']);
         $film = $this->film->create($data);
         $country_id = $this->findCountryId($row['pais'], $film);
         $film->countries()->sync($country_id);
     });
     if (!$this->newGenres->isEmpty()) {
         $this->info(Lang::get('commands.iffe.new-genres-added'));
         $this->info(implode(',', $this->newGenres->toArray()));
     }
 }
开发者ID:filmoteca,项目名称:filmoteca,代码行数:14,代码来源:ImportFilmsFromExcelCommand.php

示例2: cachePhotos

 /**
  *
  * Cache photos if caching is enabled
  *
  * @param Collection $photos
  */
 protected function cachePhotos($photos)
 {
     $cache = $this->property('cacheLifetime');
     if ($cache) {
         Cache::put('photoalbums_random_photos', $photos->toArray(), $cache);
     }
 }
开发者ID:graker,项目名称:photoalbums,代码行数:13,代码来源:RandomPhotos.php

示例3: getRawCollectionData

 private function getRawCollectionData(Collection $collection)
 {
     foreach ($collection as $element) {
         $this->execute_with_relations ? $element->loadDisplayableRelations() : null;
     }
     return $collection->toArray();
 }
开发者ID:martinsv,项目名称:JsonApi,代码行数:7,代码来源:DataHandler.php

示例4: createData

 /**
  * @param Collection $model
  * @param string $single
  *
  * @return string
  */
 public function createData($model, string $single)
 {
     $str = sprintf("\n\t\t\$scope.%s = new Minute.Models.%sArray(null);\n", $single, $this->fixName($single));
     $str .= sprintf("\t\t\$scope.%s.load(%s);\n", $single, json_encode($model->toArray(), JSON_PRETTY_PRINT));
     //{metadata: {offset: 0, limit: 2, total: %d}, items: %s}
     return $str;
 }
开发者ID:minutephp,项目名称:framework,代码行数:13,代码来源:ModelJs.php

示例5: cachePosts

 /**
  *
  * Cache posts if caching is enabled
  *
  * @param Collection $posts
  */
 protected function cachePosts($posts)
 {
     $cache = $this->property('cacheLifetime');
     if ($cache) {
         Cache::put('blogarchive_random_posts', $posts->toArray(), $cache);
     }
 }
开发者ID:graker,项目名称:blogarchive,代码行数:13,代码来源:RandomPosts.php

示例6: collectionAsItem

 public static function collectionAsItem(Collection $collection, array $columns, $cache = null)
 {
     $output = [];
     foreach ($collection->toArray() as $row) {
         $output[] = ['value' => isset($columns['value']) ? $row[$columns['value']] : $row['id'], 'text' => $row[$columns['text']]];
     }
     return $output;
 }
开发者ID:stanmay,项目名称:unflare,代码行数:8,代码来源:Database.php

示例7: formatEloquentCollection

 /**
  * Format an Eloquent collection.
  *
  * @param  \Illuminate\Database\Eloquent\Collection  $collection
  * @return string
  */
 public function formatEloquentCollection($collection)
 {
     if ($collection->isEmpty()) {
         return $this->encode([]);
     }
     $key = str_plural($collection->first()->getTable());
     return $this->encode([$key => $collection->toArray()]);
 }
开发者ID:andymrussell,项目名称:api,代码行数:14,代码来源:JsonResponseFormat.php

示例8: testToArrayCallsToArrayOnEachItemInCollection

 public function testToArrayCallsToArrayOnEachItemInCollection()
 {
     $item1 = m::mock('stdClass');
     $item1->shouldReceive('toArray')->once()->andReturn('foo.array');
     $item2 = m::mock('stdClass');
     $item2->shouldReceive('toArray')->once()->andReturn('bar.array');
     $c = new Collection(array($item1, $item2));
     $results = $c->toArray();
     $this->assertEquals(array('foo.array', 'bar.array'), $results);
 }
开发者ID:hochanh,项目名称:Bootsoft-Bowling,代码行数:10,代码来源:EloquentCollectionTest.php

示例9: toApiArray

 public function toApiArray($type = null)
 {
     $collection = parent::toArray();
     $collectionCount = count($collection);
     for ($i = 0; $i < $collectionCount; $i++) {
         switch ($type) {
             case 'notebooks':
                 break;
         }
     }
     return $collection;
 }
开发者ID:Liongold,项目名称:paperwork,代码行数:12,代码来源:paperworkCollection.php

示例10: toArray

 /**
  * Extending toArray.
  * Passing attribute you can get only an array of that elements from the collection models.
  *
  * @param string $attribute
  *
  * @return array
  */
 public function toArray($attribute = null)
 {
     $elements = parent::toArray();
     if (!$attribute) {
         return $elements;
     }
     $filtered = [];
     foreach ($elements as $element) {
         $filtered[] = $element[$attribute];
     }
     return $filtered;
 }
开发者ID:softsolution,项目名称:antVel,代码行数:20,代码来源:Collection.php

示例11: formatEloquentCollection

 /**
  * Format an Eloquent collection.
  *
  * @param \Illuminate\Database\Eloquent\Collection $collection
  *
  * @return string
  */
 public function formatEloquentCollection($collection)
 {
     if ($collection->isEmpty()) {
         return $this->encode([]);
     }
     $model = $collection->first();
     $key = str_plural($model->getTable());
     if (!$model::$snakeAttributes) {
         $key = camel_case($key);
     }
     return $this->encode([$key => $collection->toArray()]);
 }
开发者ID:joselfonseca,项目名称:api,代码行数:19,代码来源:Json.php

示例12: addRowsFromCollection

 /**
  * Using the toArray() method of the collection, adds as rows to the datatable.
  * Columns must be added with the generic addColumn() method to define the model
  * property for the column id.
  * Stolen from kevinkhill/datatableplus.
  *
  * @param Collection $collection Collection of models
  *
  * @return self
  */
 public function addRowsFromCollection(Collection $collection)
 {
     $row = [];
     $colCount = $this->lavaDataTable->getColumnCount();
     foreach ($collection->toArray() as $collectionRow) {
         $row = [];
         for ($i = 0; $i < $colCount; $i++) {
             $row[] = $collectionRow[array_keys($collectionRow)[$i]];
         }
         $this->lavaDataTable->addRow($row);
     }
     return $row;
 }
开发者ID:Bogstag,项目名称:bogstag.se,代码行数:23,代码来源:ChartController.php

示例13: familiars

 /**
  * Show the form for creating a new resource.
  *
  *
  */
 public function familiars(Request $request)
 {
     $depth = $request->query('n', 1);
     $neo4j = DB::connection('neo4j');
     /** @var $neo4j Connection */
     $client = $neo4j->getClient();
     /** @var $client Client */
     //Знаю что так нельзя, но нормально параметр вставить у меня не получилось - матюкается neo4j
     $cql = 'MATCH (x:users)-[friendship*' . $depth . ']-(y) WHERE id(x) = {id} RETURN y';
     $query = new Query($client, $cql, ['id' => Auth::user()->id]);
     $result = $query->getResultSet();
     $results = new Collection();
     foreach ($result as $row) {
         /** @var $row Row */
         $results->add(new User($row['y']->getProperties()));
     }
     return response()->json(['items' => $results->toArray()]);
 }
开发者ID:fun4life1990,项目名称:friends,代码行数:23,代码来源:FriendController.php

示例14: save

 /**
  *
  */
 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);
         }
     }
 }
开发者ID:packettide,项目名称:bree,代码行数:46,代码来源:Relate.php

示例15: var_set

 /**
  * Create recombined array with set fields
  *
  * @param Collection $data
  */
 public static function var_set(Collection $data)
 {
     $collection = new EloquentCollection();
     $model = get_class($data->first());
     foreach ($data as $item) {
         $origin = static::arrayValuesToString($item->getOriginal());
         $new = [];
         foreach ($model::$export_fields as $field => $properties) {
             if (isset($properties['relation'])) {
                 $method = $properties['relation']['method'];
                 $display = isset($properties['relation']['display']) ? $properties['relation']['display'] : null;
                 $new[] = static::getRelationItems($item, $method, $display);
             } else {
                 $new[] = $item->{$field};
             }
         }
         $collection->add($new);
     }
     return $collection->toArray();
 }
开发者ID:flo5581,项目名称:larabackend,代码行数:25,代码来源:Recombinator.php


注:本文中的Illuminate\Database\Eloquent\Collection::toArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。