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()));
     }
 }
Ejemplo n.º 2
0
 /**
  *
  * 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);
     }
 }
Ejemplo n.º 3
0
 private function getRawCollectionData(Collection $collection)
 {
     foreach ($collection as $element) {
         $this->execute_with_relations ? $element->loadDisplayableRelations() : null;
     }
     return $collection->toArray();
 }
Ejemplo n.º 4
0
 /**
  * @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;
 }
Ejemplo n.º 5
0
 /**
  *
  * 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);
     }
 }
Ejemplo n.º 6
0
 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;
 }
Ejemplo n.º 7
0
 /**
  * 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()]);
 }
 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);
 }
Ejemplo n.º 9
0
 public function toApiArray($type = null)
 {
     $collection = parent::toArray();
     $collectionCount = count($collection);
     for ($i = 0; $i < $collectionCount; $i++) {
         switch ($type) {
             case 'notebooks':
                 break;
         }
     }
     return $collection;
 }
Ejemplo n.º 10
0
 /**
  * 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;
 }
Ejemplo n.º 11
0
 /**
  * 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()]);
 }
Ejemplo n.º 12
0
 /**
  * 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;
 }
Ejemplo n.º 13
0
 /**
  * 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()]);
 }
Ejemplo n.º 14
0
 /**
  *
  */
 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);
         }
     }
 }
Ejemplo n.º 15
0
 /**
  * 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();
 }
 /**
  * the problem here is, that our polymorphic relations
  * aren't being loaded onto our data array.
  * so we need to set them manually at the end.
  *
  *
  */
 public function toArray()
 {
     list($posts, $projects) = $this->getMaps();
     $data = parent::toArray();
     return array_map(function ($widget) use($posts, $projects) {
         if ($widget['resource_type']) {
             if ($widget['resource_type'] == Post::class) {
                 $widget['resource'] = $posts->find($widget['resource_id'])->toArray();
             }
             if ($widget['resource_type'] == Project::class) {
                 $widget['resource'] = $projects->find($widget['resource_id'])->toArray();
             }
         }
         if ($widget['other_resource_type']) {
             if ($widget['other_resource_type'] == Post::class) {
                 $widget['otherResource'] = $posts->find($widget['other_resource_id'])->toArray();
             }
             if ($widget['other_resource_type'] == Project::class) {
                 $widget['otherResource'] = $projects->find($widget['other_resource_id'])->toArray();
             }
         }
         return $widget;
     }, $data);
 }
Ejemplo n.º 17
0
 /**
  * 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.
  *
  * @access public
  * @param  Collection $collection Collection of models
  * @return self
  */
 public function addRowsFromCollection(Collection $collection)
 {
     $colCount = $this->getColumnCount();
     foreach ($collection->toArray() as $collectionRow) {
         $row = [];
         for ($i = 0; $i < $colCount; $i++) {
             $row[] = $collectionRow[$this->getColumnId($i)];
         }
         $this->addRow($row);
     }
     return $this;
 }
Ejemplo n.º 18
0
 /**
  * Display one or more prospects.
  *
  * @note We're using the 'table()' function which expects an array
  *       of arrays (i.e. rows of data items/fields)
  *
  * @param Collection $prospects
  * @param bool $forceList
  * @param int $maxLen
  */
 protected function showProspects($prospects, $forceList = false, $maxLen = 76)
 {
     if (empty($prospects) || $prospects->isEmpty()) {
         $this->info('No prospect records to display');
         return;
     }
     if ($prospects->count() > 1 || $forceList) {
         $rows = $prospects->toArray();
         $this->table(array_keys($rows[0]), $rows);
     } else {
         $prospect = $prospects->first();
         $data = $prospect->toArray();
         $rows = [];
         foreach ($data as $k => $v) {
             $rows[] = [$k, is_string($v) ? str_limit($v, $maxLen) : $v];
         }
         $this->table(['field', 'value'], $rows);
     }
     $this->info('');
     // Print a blank line
 }
Ejemplo n.º 19
0
 /**
  * Creates a CSV String from the given collection.
  *
  * Modified version of: https://gist.github.com/johanmeiring/2894568
  *
  * @param Collection|array $input
  * @throws \InvalidArgumentException
  * @return string
  */
 public function collectionToCSVString($input)
 {
     $converted = [];
     if ($input instanceof Collection) {
         $converted = $input->toArray();
     } elseif (is_array($input)) {
         $converted = $input;
     } else {
         throw new \InvalidArgumentException();
     }
     if (count($converted) == 0) {
         return '';
     }
     $converter = new CSVConverter(array_keys($converted[0]));
     foreach ($converted as $row) {
         $converter->addRow($row);
     }
     return (string) $converter;
 }
Ejemplo n.º 20
0
 /**
  * Constructor.
  *
  * @param Illuminate\Database\Eloquent\Collection $collection - a collection to present.
  * @access public
  */
 public function __construct(Collection $collection)
 {
     parent::__construct($collection->toArray());
 }
Ejemplo n.º 21
0
 /**
  * Serialize intance's relations.
  *
  * @param \Illuminate\Support\Contracts\ArrayableInterface $instance
  * @return array
  */
 public function serializeRelations(ArrayableInterface $instance)
 {
     $result = new Collection();
     if (!$this->isCollection($instance)) {
         $collection = new Collection();
         $instance = $collection->add($instance);
     }
     $sub_result = $this->collectRelations($instance);
     $sub_result = $this->mergeRelations($sub_result);
     foreach ($sub_result as $id => $value) {
         $result->put($id, $value);
     }
     return $result->toArray();
 }
 /**
  * Exports Eloquent collections as an array
  *
  * @param Collection $resultSet
  * @return array
  */
 protected function exportEloquentCollection(Collection $resultSet)
 {
     return ['data' => $resultSet->toArray()];
 }