/** * Get an array with the values of a given column. * * @param string $column * @param string $key * @return array */ public function lists($column, $key = null) { $columns = $this->getListSelect($column, $key); // First we will just get all of the column values for the record result set // then we can associate those values with the column if it was specified // otherwise we can just give these values back without a specific key. $res = $this->get($columns); $results = new Collection($res->getData()); $values = $results->fetch($columns[0])->all(); // If a key was specified and we have results, we will go ahead and combine // the values with the keys of all of the records so that the values can // be accessed by the key of the rows instead of simply being numeric. if (!is_null($key) && count($results) > 0) { $keys = $results->fetch($key)->all(); return array_combine($keys, $values); } return $values; }
/** * 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->fetch($columns[1])->all(); $indentation = $results->fetch($columns[0])->all(); foreach ($values as $_key => $value) { $values[$_key] = str_repeat($indent, $indentation[$_key]) . $value; } if ($key !== null && count($results) > 0) { $keys = $results->fetch($key)->all(); return array_combine($keys, $values); } return $values; }