Skip to content

Commit cd75b17

Browse files
authored
Merge pull request #925 from pi0/5.3
Initial support laravel 5.3
2 parents 78a5a05 + 0f14212 commit cd75b17

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ composer require jenssegers/mongodb
3939
5.0.x | 2.1.x
4040
5.1.x | 2.2.x or 3.0.x
4141
5.2.x | 2.3.x or 3.0.x
42+
5.3.x | 3.0.x
4243

4344
And add the service provider in `config/app.php`:
4445

src/Jenssegers/Mongodb/Eloquent/Builder.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Builder extends EloquentBuilder
1313
* @var array
1414
*/
1515
protected $passthru = [
16-
'toSql', 'lists', 'insert', 'insertGetId', 'pluck',
16+
'toSql', 'insert', 'insertGetId', 'pluck',
1717
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull',
1818
];
1919

@@ -167,7 +167,9 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o
167167
$query = $hasQuery->getQuery();
168168

169169
// Get the number of related objects for each possible parent.
170-
$relationCount = array_count_values($query->lists($relation->getHasCompareKey()));
170+
$relationCount = array_count_values(array_map(function ($id) {
171+
return (string) $id; // Convert Back ObjectIds to Strings
172+
}, $query->pluck($relation->getHasCompareKey())));
171173

172174
// Remove unwanted related objects based on the operator and count.
173175
$relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) {

src/Jenssegers/Mongodb/Query/Builder.php

+18-15
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ class Builder extends BaseBuilder
7878
'>=' => '$gte',
7979
];
8080

81+
/**
82+
* Check if we need to return Collections instead of plain arrays (laravel >= 5.3 )
83+
*
84+
* @var boolean
85+
*/
86+
protected $useCollections;
87+
8188
/**
8289
* Create a new query builder instance.
8390
*
@@ -89,6 +96,7 @@ public function __construct(Connection $connection, Processor $processor)
8996
$this->grammar = new Grammar;
9097
$this->connection = $connection;
9198
$this->processor = $processor;
99+
$this->useCollections = version_compare(\Illuminate\Foundation\Application::VERSION, '5.3', '>=');
92100
}
93101

94102
/**
@@ -146,7 +154,7 @@ public function find($id, $columns = [])
146154
* Execute the query as a "select" statement.
147155
*
148156
* @param array $columns
149-
* @return array|static[]
157+
* @return array|static[]|Collection
150158
*/
151159
public function get($columns = [])
152160
{
@@ -157,7 +165,7 @@ public function get($columns = [])
157165
* Execute the query as a fresh "select" statement.
158166
*
159167
* @param array $columns
160-
* @return array|static[]
168+
* @return array|static[]|Collection
161169
*/
162170
public function getFresh($columns = [])
163171
{
@@ -259,7 +267,7 @@ public function getFresh($columns = [])
259267
$results = iterator_to_array($this->collection->aggregate($pipeline, $options));
260268

261269
// Return results
262-
return $results;
270+
return $this->useCollections ? new Collection($results) : $results;
263271
}
264272

265273
// Distinct query
@@ -274,7 +282,7 @@ public function getFresh($columns = [])
274282
$result = $this->collection->distinct($column);
275283
}
276284

277-
return $result;
285+
return $this->useCollections ? new Collection($result) : $result;
278286
}
279287

280288
// Normal query
@@ -317,7 +325,8 @@ public function getFresh($columns = [])
317325
$cursor = $this->collection->find($wheres, $options);
318326

319327
// Return results as an array with numeric keys
320-
return iterator_to_array($cursor, false);
328+
$results = iterator_to_array($cursor, false);
329+
return $this->useCollections ? new Collection($results) : $results;
321330
}
322331
}
323332

@@ -568,14 +577,7 @@ public function pluck($column, $key = null)
568577
{
569578
$results = $this->get(is_null($key) ? [$column] : [$column, $key]);
570579

571-
// If the columns are qualified with a table or have an alias, we cannot use
572-
// those directly in the "pluck" operations since the results from the DB
573-
// are only keyed by the column itself. We'll strip the table out here.
574-
return Arr::pluck(
575-
$results,
576-
$column,
577-
$key
578-
);
580+
return $this->useCollections ? $results->pluck($column, $key) : Arr::pluck($results, $column, $key);
579581
}
580582

581583
/**
@@ -623,6 +625,7 @@ public function truncate()
623625
/**
624626
* Get an array with the values of a given column.
625627
*
628+
* @deprecated
626629
* @param string $column
627630
* @param string $key
628631
* @return array
@@ -639,10 +642,10 @@ public function lists($column, $key = null)
639642
return $item;
640643
});
641644

642-
return $results->lists($column, $key)->all();
645+
return $results->pluck($column, $key)->all();
643646
}
644647

645-
return parent::lists($column, $key);
648+
return parent::pluck($column, $key);
646649
}
647650

648651
/**

0 commit comments

Comments
 (0)