Skip to content

Support laravel 5.3 #925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Aug 29, 2016
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand Down
6 changes: 4 additions & 2 deletions src/Jenssegers/Mongodb/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

Expand Down Expand Up @@ -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) {
Expand Down
33 changes: 18 additions & 15 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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', '>=');
}

/**
Expand Down Expand Up @@ -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 = [])
{
Expand All @@ -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 = [])
{
Expand Down Expand Up @@ -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
Expand All @@ -274,7 +282,7 @@ public function getFresh($columns = [])
$result = $this->collection->distinct($column);
}

return $result;
return $this->useCollections ? new Collection($result) : $result;
}

// Normal query
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down