/** * Group an array based on one of it's values. * * @method scopeArrayGrouped * * @param Illuminate\Database\Query\Builder $query The query being scoped * @param string $group_key The key whose value we'll group by * * @return array The grouped array */ public function scopeArrayGrouped($query, $key_column) { // First get the results from the query $results = $query->get(); // Initialise an empty array to store our results in $rtn = []; /** @var Illuminate\Database\Eloquent\Model $result */ foreach ($results as $result) { $group_key = $result->{$key_column}; // If the group key has not already been set in our results, // initialise it as an empty array if (!isset($rtn[$group_key])) { $rtn[$group_key] = []; } // Add the model to the query $rtn[$group_key][] = $result; } return $rtn; }
/** * Shortcut for get+toJson methods. * @param string $columns columns * @return string */ public function toJson($columns = array('*')) { return $this->query->get($columns)->toJson(); }