diff --git a/README.md b/README.md index 36d8e4152..8996c7aba 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ composer require jenssegers/mongodb 5.0.x | 2.1.x 5.1.x | 2.2.x or 3.0.x 5.2.x | 2.3.x or 3.0.x + 5.3.x | 3.0.x And add the service provider in `config/app.php`: diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index 3e21468a6..745c73476 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -13,7 +13,7 @@ class Builder extends EloquentBuilder * @var array */ protected $passthru = [ - 'toSql', 'lists', 'insert', 'insertGetId', 'pluck', + 'toSql', 'insert', 'insertGetId', 'pluck', 'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull', ]; @@ -167,7 +167,9 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o $query = $hasQuery->getQuery(); // Get the number of related objects for each possible parent. - $relationCount = array_count_values($query->lists($relation->getHasCompareKey())); + $relationCount = array_count_values(array_map(function ($id) { + return (string) $id; // Convert Back ObjectIds to Strings + }, $query->pluck($relation->getHasCompareKey()))); // Remove unwanted related objects based on the operator and count. $relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) { diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index d7a60d4d1..709c373ce 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -78,6 +78,13 @@ class Builder extends BaseBuilder '>=' => '$gte', ]; + /** + * Check if we need to return Collections instead of plain arrays (laravel >= 5.3 ) + * + * @var boolean + */ + protected $useCollections; + /** * Create a new query builder instance. * @@ -89,6 +96,7 @@ public function __construct(Connection $connection, Processor $processor) $this->grammar = new Grammar; $this->connection = $connection; $this->processor = $processor; + $this->useCollections = version_compare(\Illuminate\Foundation\Application::VERSION, '5.3', '>='); } /** @@ -146,7 +154,7 @@ public function find($id, $columns = []) * Execute the query as a "select" statement. * * @param array $columns - * @return array|static[] + * @return array|static[]|Collection */ public function get($columns = []) { @@ -157,7 +165,7 @@ public function get($columns = []) * Execute the query as a fresh "select" statement. * * @param array $columns - * @return array|static[] + * @return array|static[]|Collection */ public function getFresh($columns = []) { @@ -259,7 +267,7 @@ public function getFresh($columns = []) $results = iterator_to_array($this->collection->aggregate($pipeline, $options)); // Return results - return $results; + return $this->useCollections ? new Collection($results) : $results; } // Distinct query @@ -274,7 +282,7 @@ public function getFresh($columns = []) $result = $this->collection->distinct($column); } - return $result; + return $this->useCollections ? new Collection($result) : $result; } // Normal query @@ -317,7 +325,8 @@ public function getFresh($columns = []) $cursor = $this->collection->find($wheres, $options); // Return results as an array with numeric keys - return iterator_to_array($cursor, false); + $results = iterator_to_array($cursor, false); + return $this->useCollections ? new Collection($results) : $results; } } @@ -568,14 +577,7 @@ public function pluck($column, $key = null) { $results = $this->get(is_null($key) ? [$column] : [$column, $key]); - // If the columns are qualified with a table or have an alias, we cannot use - // those directly in the "pluck" operations since the results from the DB - // are only keyed by the column itself. We'll strip the table out here. - return Arr::pluck( - $results, - $column, - $key - ); + return $this->useCollections ? $results->pluck($column, $key) : Arr::pluck($results, $column, $key); } /** @@ -623,6 +625,7 @@ public function truncate() /** * Get an array with the values of a given column. * + * @deprecated * @param string $column * @param string $key * @return array @@ -639,10 +642,10 @@ public function lists($column, $key = null) return $item; }); - return $results->lists($column, $key)->all(); + return $results->pluck($column, $key)->all(); } - return parent::lists($column, $key); + return parent::pluck($column, $key); } /**