@@ -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