@@ -78,6 +78,13 @@ class Builder extends BaseBuilder
78
78
'>= ' => '$gte ' ,
79
79
];
80
80
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
+
81
88
/**
82
89
* Create a new query builder instance.
83
90
*
@@ -89,6 +96,7 @@ public function __construct(Connection $connection, Processor $processor)
89
96
$ this ->grammar = new Grammar ;
90
97
$ this ->connection = $ connection ;
91
98
$ this ->processor = $ processor ;
99
+ $ this ->useCollections = version_compare (\Illuminate \Foundation \Application::VERSION , '5.3 ' , '>= ' );
92
100
}
93
101
94
102
/**
@@ -146,7 +154,7 @@ public function find($id, $columns = [])
146
154
* Execute the query as a "select" statement.
147
155
*
148
156
* @param array $columns
149
- * @return array|static[]
157
+ * @return array|static[]|Collection
150
158
*/
151
159
public function get ($ columns = [])
152
160
{
@@ -157,7 +165,7 @@ public function get($columns = [])
157
165
* Execute the query as a fresh "select" statement.
158
166
*
159
167
* @param array $columns
160
- * @return array|static[]
168
+ * @return array|static[]|Collection
161
169
*/
162
170
public function getFresh ($ columns = [])
163
171
{
@@ -259,7 +267,7 @@ public function getFresh($columns = [])
259
267
$ results = iterator_to_array ($ this ->collection ->aggregate ($ pipeline , $ options ));
260
268
261
269
// Return results
262
- return $ results ;
270
+ return $ this -> useCollections ? new Collection ( $ results ) : $ results ;
263
271
}
264
272
265
273
// Distinct query
@@ -274,7 +282,7 @@ public function getFresh($columns = [])
274
282
$ result = $ this ->collection ->distinct ($ column );
275
283
}
276
284
277
- return $ result ;
285
+ return $ this -> useCollections ? new Collection ( $ result ) : $ result ;
278
286
}
279
287
280
288
// Normal query
@@ -317,7 +325,8 @@ public function getFresh($columns = [])
317
325
$ cursor = $ this ->collection ->find ($ wheres , $ options );
318
326
319
327
// 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 ;
321
330
}
322
331
}
323
332
@@ -568,14 +577,7 @@ public function pluck($column, $key = null)
568
577
{
569
578
$ results = $ this ->get (is_null ($ key ) ? [$ column ] : [$ column , $ key ]);
570
579
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 );
579
581
}
580
582
581
583
/**
@@ -623,6 +625,7 @@ public function truncate()
623
625
/**
624
626
* Get an array with the values of a given column.
625
627
*
628
+ * @deprecated
626
629
* @param string $column
627
630
* @param string $key
628
631
* @return array
@@ -639,10 +642,10 @@ public function lists($column, $key = null)
639
642
return $ item ;
640
643
});
641
644
642
- return $ results ->lists ($ column , $ key )->all ();
645
+ return $ results ->pluck ($ column , $ key )->all ();
643
646
}
644
647
645
- return parent ::lists ($ column , $ key );
648
+ return parent ::pluck ($ column , $ key );
646
649
}
647
650
648
651
/**
0 commit comments