Skip to content

Commit f07eb5a

Browse files
committed
PHPORM-239 Apply result transformation to raw() queries
1 parent ce97048 commit f07eb5a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Eloquent/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace MongoDB\Laravel\Eloquent;
66

77
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8-
use MongoDB\Driver\Cursor;
8+
use MongoDB\Driver\CursorInterface;
99
use MongoDB\Driver\Exception\WriteException;
1010
use MongoDB\Laravel\Connection;
1111
use MongoDB\Laravel\Helpers\QueriesRelationships;
@@ -177,7 +177,7 @@ public function raw($value = null)
177177
$results = $this->query->raw($value);
178178

179179
// Convert MongoCursor results to a collection of models.
180-
if ($results instanceof Cursor) {
180+
if ($results instanceof CursorInterface) {
181181
$results = iterator_to_array($results, false);
182182

183183
return $this->model->hydrate($results);

src/Query/Builder.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use MongoDB\BSON\UTCDateTime;
2626
use MongoDB\Builder\Stage\FluentFactoryTrait;
2727
use MongoDB\Driver\Cursor;
28+
use MongoDB\Driver\CursorInterface;
2829
use Override;
2930
use RuntimeException;
3031
use stdClass;
@@ -934,7 +935,17 @@ public function raw($value = null)
934935
{
935936
// Execute the closure on the mongodb collection
936937
if ($value instanceof Closure) {
937-
return call_user_func($value, $this->collection);
938+
$results = call_user_func($value, $this->collection);
939+
940+
if ($results instanceof CursorInterface) {
941+
$results = $results->toArray();
942+
}
943+
944+
if (is_array($results) || is_object($results)) {
945+
$results = $this->aliasIdForResult($results);
946+
}
947+
948+
return $results;
938949
}
939950

940951
// Create an expression for the given value

0 commit comments

Comments
 (0)