Exemple #1
0
 /**
  * Gets an array with values of a given column. Values are indented according to their depth.
  * @param  string $column Array values
  * @param  string $key    Array keys
  * @param  string $indent Character to indent depth
  * @return array
  */
 public function scopeListsNested($query, $column, $key = null, $indent = '   ')
 {
     $columns = [$this->getDepthColumnName(), $column];
     if ($key !== null) {
         $columns[] = $key;
     }
     $results = new Collection($query->getQuery()->get($columns));
     $values = $results->pluck($columns[1])->all();
     $indentation = $results->pluck($columns[0])->all();
     if (count($values) !== count($indentation)) {
         throw new Exception('Column mismatch in listsNested method. Are you sure the columns exist?');
     }
     foreach ($values as $_key => $value) {
         $values[$_key] = str_repeat($indent, $indentation[$_key]) . $value;
     }
     if ($key !== null && count($results) > 0) {
         $keys = $results->pluck($key)->all();
         return array_combine($keys, $values);
     }
     return $values;
 }