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
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`:
......
......@@ -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 @@ class Builder extends EloquentBuilder
$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) {
......
......@@ -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 @@ class Builder extends BaseBuilder
$this->grammar = new Grammar;
$this->connection = $connection;
$this->processor = $processor;
$this->useCollections = version_compare(\Illuminate\Foundation\Application::VERSION, '5.3', '>=');
}
/**
......@@ -146,7 +154,7 @@ class Builder extends BaseBuilder
* 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 @@ class Builder extends BaseBuilder
* 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 @@ class Builder extends BaseBuilder
$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 @@ class Builder extends BaseBuilder
$result = $this->collection->distinct($column);
}
return $result;
return $this->useCollections ? new Collection($result) : $result;
}
// Normal query
......@@ -317,7 +325,8 @@ class Builder extends BaseBuilder
$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 @@ class Builder extends BaseBuilder
{
$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 @@ class Builder extends BaseBuilder
/**
* Get an array with the values of a given column.
*
* @deprecated
* @param string $column
* @param string $key
* @return array
......@@ -639,10 +642,10 @@ class Builder extends BaseBuilder
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