本文整理汇总了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()));
}
}
示例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);
}
}
示例3: getRawCollectionData
private function getRawCollectionData(Collection $collection)
{
foreach ($collection as $element) {
$this->execute_with_relations ? $element->loadDisplayableRelations() : null;
}
return $collection->toArray();
}
示例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;
}
示例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);
}
}
示例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;
}
示例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()]);
}
示例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);
}
示例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;
}
示例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;
}
示例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()]);
}
示例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;
}
示例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()]);
}
示例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);
}
}
}
示例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();
}