Commit cd75b176 authored by Jens Segers's avatar Jens Segers Committed by GitHub

Merge pull request #925 from pi0/5.3

Initial support laravel 5.3
parents 78a5a056 0f142129
...@@ -39,6 +39,7 @@ composer require jenssegers/mongodb ...@@ -39,6 +39,7 @@ composer require jenssegers/mongodb
5.0.x | 2.1.x 5.0.x | 2.1.x
5.1.x | 2.2.x or 3.0.x 5.1.x | 2.2.x or 3.0.x
5.2.x | 2.3.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`: And add the service provider in `config/app.php`:
......
...@@ -13,7 +13,7 @@ class Builder extends EloquentBuilder ...@@ -13,7 +13,7 @@ class Builder extends EloquentBuilder
* @var array * @var array
*/ */
protected $passthru = [ protected $passthru = [
'toSql', 'lists', 'insert', 'insertGetId', 'pluck', 'toSql', 'insert', 'insertGetId', 'pluck',
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull', 'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull',
]; ];
...@@ -167,7 +167,9 @@ class Builder extends EloquentBuilder ...@@ -167,7 +167,9 @@ class Builder extends EloquentBuilder
$query = $hasQuery->getQuery(); $query = $hasQuery->getQuery();
// Get the number of related objects for each possible parent. // 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. // Remove unwanted related objects based on the operator and count.
$relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) { $relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) {
......
...@@ -78,6 +78,13 @@ class Builder extends BaseBuilder ...@@ -78,6 +78,13 @@ class Builder extends BaseBuilder
'>=' => '$gte', '>=' => '$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. * Create a new query builder instance.
* *
...@@ -89,6 +96,7 @@ class Builder extends BaseBuilder ...@@ -89,6 +96,7 @@ class Builder extends BaseBuilder
$this->grammar = new Grammar; $this->grammar = new Grammar;
$this->connection = $connection; $this->connection = $connection;
$this->processor = $processor; $this->processor = $processor;
$this->useCollections = version_compare(\Illuminate\Foundation\Application::VERSION, '5.3', '>=');
} }
/** /**
...@@ -146,7 +154,7 @@ class Builder extends BaseBuilder ...@@ -146,7 +154,7 @@ class Builder extends BaseBuilder
* Execute the query as a "select" statement. * Execute the query as a "select" statement.
* *
* @param array $columns * @param array $columns
* @return array|static[] * @return array|static[]|Collection
*/ */
public function get($columns = []) public function get($columns = [])
{ {
...@@ -157,7 +165,7 @@ class Builder extends BaseBuilder ...@@ -157,7 +165,7 @@ class Builder extends BaseBuilder
* Execute the query as a fresh "select" statement. * Execute the query as a fresh "select" statement.
* *
* @param array $columns * @param array $columns
* @return array|static[] * @return array|static[]|Collection
*/ */
public function getFresh($columns = []) public function getFresh($columns = [])
{ {
...@@ -259,7 +267,7 @@ class Builder extends BaseBuilder ...@@ -259,7 +267,7 @@ class Builder extends BaseBuilder
$results = iterator_to_array($this->collection->aggregate($pipeline, $options)); $results = iterator_to_array($this->collection->aggregate($pipeline, $options));
// Return results // Return results
return $results; return $this->useCollections ? new Collection($results) : $results;
} }
// Distinct query // Distinct query
...@@ -274,7 +282,7 @@ class Builder extends BaseBuilder ...@@ -274,7 +282,7 @@ class Builder extends BaseBuilder
$result = $this->collection->distinct($column); $result = $this->collection->distinct($column);
} }
return $result; return $this->useCollections ? new Collection($result) : $result;
} }
// Normal query // Normal query
...@@ -317,7 +325,8 @@ class Builder extends BaseBuilder ...@@ -317,7 +325,8 @@ class Builder extends BaseBuilder
$cursor = $this->collection->find($wheres, $options); $cursor = $this->collection->find($wheres, $options);
// Return results as an array with numeric keys // 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 @@ class Builder extends BaseBuilder ...@@ -568,14 +577,7 @@ class Builder extends BaseBuilder
{ {
$results = $this->get(is_null($key) ? [$column] : [$column, $key]); $results = $this->get(is_null($key) ? [$column] : [$column, $key]);
// If the columns are qualified with a table or have an alias, we cannot use return $this->useCollections ? $results->pluck($column, $key) : Arr::pluck($results, $column, $key);
// 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
);
} }
/** /**
...@@ -623,6 +625,7 @@ class Builder extends BaseBuilder ...@@ -623,6 +625,7 @@ class Builder extends BaseBuilder
/** /**
* Get an array with the values of a given column. * Get an array with the values of a given column.
* *
* @deprecated
* @param string $column * @param string $column
* @param string $key * @param string $key
* @return array * @return array
...@@ -639,10 +642,10 @@ class Builder extends BaseBuilder ...@@ -639,10 +642,10 @@ class Builder extends BaseBuilder
return $item; return $item;
}); });
return $results->lists($column, $key)->all(); return $results->pluck($column, $key)->all();
} }
return parent::lists($column, $key); return parent::pluck($column, $key);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment