diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index 5d5be92e936f..69780f78a2b4 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -389,12 +389,11 @@ public function selectFromWriteConnection($query, $bindings = []) * @param string $query * @param array $bindings * @param bool $useReadPdo - * @param array $fetchUsing * @return array */ - public function select($query, $bindings = [], $useReadPdo = true, array $fetchUsing = []) + public function select($query, $bindings = [], $useReadPdo = true) { - return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo, $fetchUsing) { + return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) { if ($this->pretending()) { return []; } @@ -410,7 +409,7 @@ public function select($query, $bindings = [], $useReadPdo = true, array $fetchU $statement->execute(); - return $statement->fetchAll(...$fetchUsing); + return $statement->fetchAll(); }); } @@ -420,12 +419,11 @@ public function select($query, $bindings = [], $useReadPdo = true, array $fetchU * @param string $query * @param array $bindings * @param bool $useReadPdo - * @param array $fetchUsing * @return array */ - public function selectResultSets($query, $bindings = [], $useReadPdo = true, array $fetchUsing = []) + public function selectResultSets($query, $bindings = [], $useReadPdo = true) { - return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo, $fetchUsing) { + return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) { if ($this->pretending()) { return []; } @@ -441,7 +439,7 @@ public function selectResultSets($query, $bindings = [], $useReadPdo = true, arr $sets = []; do { - $sets[] = $statement->fetchAll(...$fetchUsing); + $sets[] = $statement->fetchAll(); } while ($statement->nextRowset()); return $sets; @@ -454,10 +452,9 @@ public function selectResultSets($query, $bindings = [], $useReadPdo = true, arr * @param string $query * @param array $bindings * @param bool $useReadPdo - * @param array $fetchUsing * @return \Generator */ - public function cursor($query, $bindings = [], $useReadPdo = true, array $fetchUsing = []) + public function cursor($query, $bindings = [], $useReadPdo = true) { $statement = $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) { if ($this->pretending()) { @@ -482,7 +479,7 @@ public function cursor($query, $bindings = [], $useReadPdo = true, array $fetchU return $statement; }); - while ($record = $statement->fetch(...$fetchUsing)) { + while ($record = $statement->fetch()) { yield $record; } } diff --git a/src/Illuminate/Database/ConnectionInterface.php b/src/Illuminate/Database/ConnectionInterface.php index 22f866b43763..288adb4206e3 100755 --- a/src/Illuminate/Database/ConnectionInterface.php +++ b/src/Illuminate/Database/ConnectionInterface.php @@ -51,10 +51,9 @@ public function scalar($query, $bindings = [], $useReadPdo = true); * @param string $query * @param array $bindings * @param bool $useReadPdo - * @param array $fetchUsing * @return array */ - public function select($query, $bindings = [], $useReadPdo = true, array $fetchUsing = []); + public function select($query, $bindings = [], $useReadPdo = true); /** * Run a select statement against the database and returns a generator. @@ -62,10 +61,9 @@ public function select($query, $bindings = [], $useReadPdo = true, array $fetchU * @param string $query * @param array $bindings * @param bool $useReadPdo - * @param array $fetchUsing * @return \Generator */ - public function cursor($query, $bindings = [], $useReadPdo = true, array $fetchUsing = []); + public function cursor($query, $bindings = [], $useReadPdo = true); /** * Run an insert statement against the database. diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 0266f24cfe69..c5db6b31060e 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -249,13 +249,6 @@ class Builder implements BuilderContract */ public $useWritePdo = false; - /** - * The custom arguments for the PDOStatement::fetchAll / fetch functions. - * - * @var array - */ - public array $fetchUsing = []; - /** * Create a new query builder instance. * @@ -3094,13 +3087,9 @@ public function soleValue($column) */ public function get($columns = ['*']) { - $original = $this->columns; - - $this->columns ??= Arr::wrap($columns); - - $items = new Collection($this->processor->processSelect($this, $this->runSelect())); - - $this->columns = $original; + $items = new Collection($this->onceWithColumns(Arr::wrap($columns), function () { + return $this->processor->processSelect($this, $this->runSelect()); + })); return $this->applyAfterQueryCallbacks( isset($this->groupLimit) ? $this->withoutGroupLimitKeys($items) : $items @@ -3115,7 +3104,7 @@ public function get($columns = ['*']) protected function runSelect() { return $this->connection->select( - $this->toSql(), $this->getBindings(), ! $this->useWritePdo, $this->fetchUsing + $this->toSql(), $this->getBindings(), ! $this->useWritePdo ); } @@ -3333,7 +3322,7 @@ public function cursor() return (new LazyCollection(function () { yield from $this->connection->cursor( - $this->toSql(), $this->getBindings(), ! $this->useWritePdo, $this->fetchUsing + $this->toSql(), $this->getBindings(), ! $this->useWritePdo ); }))->map(function ($item) { return $this->applyAfterQueryCallbacks(new Collection([$item]))->first(); @@ -3363,18 +3352,17 @@ protected function enforceOrderBy() */ public function pluck($column, $key = null) { - $original = $this->columns; - // First, we will need to select the results of the query accounting for the // given columns / key. Once we have the results, we will be able to take // the results and get the exact data that was requested for the query. - $this->columns = is_null($key) || $key === $column - ? [$column] - : [$column, $key]; - - $queryResult = $this->processor->processSelect($this, $this->runSelect()); - - $this->columns = $original; + $queryResult = $this->onceWithColumns( + is_null($key) || $key === $column ? [$column] : [$column, $key], + function () { + return $this->processor->processSelect( + $this, $this->runSelect() + ); + } + ); if (empty($queryResult)) { return new Collection; @@ -3668,6 +3656,30 @@ protected function setAggregate($function, $columns) return $this; } + /** + * Execute the given callback while selecting the given columns. + * + * After running the callback, the columns are reset to the original value. + * + * @param array $columns + * @param callable $callback + * @return mixed + */ + protected function onceWithColumns($columns, $callback) + { + $original = $this->columns; + + if (is_null($original)) { + $this->columns = $columns; + } + + $result = $callback(); + + $this->columns = $original; + + return $result; + } + /** * Insert new records into the database. * @@ -4281,19 +4293,6 @@ public function useWritePdo() return $this; } - /** - * Specify arguments for the PDOStatement::fetchAll / fetch functions. - * - * @param mixed ...$fetchUsing - * @return $this - */ - public function fetchUsing(...$fetchUsing) - { - $this->fetchUsing = $fetchUsing; - - return $this; - } - /** * Determine if the value is a query builder instance or a Closure. * diff --git a/tests/Database/DatabaseEloquentBelongsToManyCreateOrFirstTest.php b/tests/Database/DatabaseEloquentBelongsToManyCreateOrFirstTest.php index 886bc47ef6c4..8fb7dab36607 100644 --- a/tests/Database/DatabaseEloquentBelongsToManyCreateOrFirstTest.php +++ b/tests/Database/DatabaseEloquentBelongsToManyCreateOrFirstTest.php @@ -84,7 +84,7 @@ public function testCreateOrFirstMethodAssociatesExistingRelated(): void $source->getConnection() ->expects('select') - ->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([[ 'id' => 456, 'attr' => 'foo', @@ -128,7 +128,6 @@ public function testFirstOrCreateMethodRetrievesExistingRelatedAlreadyAssociated 'select "related_table".*, "pivot_table"."source_id" as "pivot_source_id", "pivot_table"."related_id" as "pivot_related_id" from "related_table" inner join "pivot_table" on "related_table"."id" = "pivot_table"."related_id" where "pivot_table"."source_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([[ 'id' => 456, @@ -177,7 +176,7 @@ public function testCreateOrFirstMethodRetrievesExistingRelatedAssociatedJustNow $source->getConnection() ->expects('select') - ->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([[ 'id' => 456, 'attr' => 'foo', @@ -200,7 +199,6 @@ public function testCreateOrFirstMethodRetrievesExistingRelatedAssociatedJustNow 'select "related_table".*, "pivot_table"."source_id" as "pivot_source_id", "pivot_table"."related_id" as "pivot_related_id" from "related_table" inner join "pivot_table" on "related_table"."id" = "pivot_table"."related_id" where "pivot_table"."source_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], false, - [], ) ->andReturn([[ 'id' => 456, @@ -245,7 +243,6 @@ public function testFirstOrCreateMethodRetrievesExistingRelatedAndAssociatesIt() 'select "related_table".*, "pivot_table"."source_id" as "pivot_source_id", "pivot_table"."related_id" as "pivot_related_id" from "related_table" inner join "pivot_table" on "related_table"."id" = "pivot_table"."related_id" where "pivot_table"."source_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([]); @@ -255,7 +252,6 @@ public function testFirstOrCreateMethodRetrievesExistingRelatedAndAssociatesIt() 'select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true, - [], ) ->andReturn([[ 'id' => 456, @@ -330,7 +326,6 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore 'select "related_table".*, "pivot_table"."source_id" as "pivot_source_id", "pivot_table"."related_id" as "pivot_related_id" from "related_table" inner join "pivot_table" on "related_table"."id" = "pivot_table"."related_id" where "pivot_table"."source_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([]); @@ -340,7 +335,6 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore 'select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true, - [], ) ->andReturn([]); diff --git a/tests/Database/DatabaseEloquentBuilderCreateOrFirstTest.php b/tests/Database/DatabaseEloquentBuilderCreateOrFirstTest.php index dcf9d912fe79..63cffe311f53 100755 --- a/tests/Database/DatabaseEloquentBuilderCreateOrFirstTest.php +++ b/tests/Database/DatabaseEloquentBuilderCreateOrFirstTest.php @@ -66,7 +66,7 @@ public function testCreateOrFirstMethodRetrievesExistingRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], false, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], false) ->andReturn([[ 'id' => 123, 'attr' => 'foo', @@ -95,7 +95,7 @@ public function testFirstOrCreateMethodRetrievesExistingRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([[ 'id' => 123, 'attr' => 'foo', @@ -124,7 +124,7 @@ public function testFirstOrCreateMethodCreatesNewRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([]); $model->getConnection()->expects('insert')->with( @@ -152,7 +152,7 @@ public function testFirstOrCreateMethodRetrievesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([]); $sql = 'insert into "table" ("attr", "val", "updated_at", "created_at") values (?, ?, ?, ?)'; @@ -165,7 +165,7 @@ public function testFirstOrCreateMethodRetrievesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], false, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], false) ->andReturn([[ 'id' => 123, 'attr' => 'foo', @@ -194,7 +194,7 @@ public function testUpdateOrCreateMethodUpdatesExistingRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([[ 'id' => 123, 'attr' => 'foo', @@ -231,7 +231,7 @@ public function testUpdateOrCreateMethodCreatesNewRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([]); $model->getConnection()->expects('insert')->with( @@ -259,7 +259,7 @@ public function testUpdateOrCreateMethodUpdatesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([]); $sql = 'insert into "table" ("attr", "val", "updated_at", "created_at") values (?, ?, ?, ?)'; @@ -272,7 +272,7 @@ public function testUpdateOrCreateMethodUpdatesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], false, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], false) ->andReturn([[ 'id' => 123, 'attr' => 'foo', @@ -309,7 +309,7 @@ public function testIncrementOrCreateMethodIncrementsExistingRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([[ 'id' => 123, 'attr' => 'foo', @@ -351,7 +351,7 @@ public function testIncrementOrCreateMethodCreatesNewRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([]); $model->getConnection()->expects('insert')->with( @@ -379,7 +379,7 @@ public function testIncrementOrCreateMethodIncrementParametersArePassed(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([[ 'id' => 123, 'attr' => 'foo', @@ -423,7 +423,7 @@ public function testIncrementOrCreateMethodRetrievesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], true) ->andReturn([]); $sql = 'insert into "table" ("attr", "count", "updated_at", "created_at") values (?, ?, ?, ?)'; @@ -436,7 +436,7 @@ public function testIncrementOrCreateMethodRetrievesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], false, []) + ->with('select * from "table" where ("attr" = ?) limit 1', ['foo'], false) ->andReturn([[ 'id' => 123, 'attr' => 'foo', diff --git a/tests/Database/DatabaseEloquentHasManyCreateOrFirstTest.php b/tests/Database/DatabaseEloquentHasManyCreateOrFirstTest.php index 38802b80f335..5a3b9e5c1c06 100755 --- a/tests/Database/DatabaseEloquentHasManyCreateOrFirstTest.php +++ b/tests/Database/DatabaseEloquentHasManyCreateOrFirstTest.php @@ -70,7 +70,7 @@ public function testCreateOrFirstMethodRetrievesExistingRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], false, []) + ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], false) ->andReturn([[ 'id' => 456, 'parent_id' => 123, @@ -102,7 +102,7 @@ public function testFirstOrCreateMethodCreatesNewRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true, []) + ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true) ->andReturn([]); $model->getConnection()->expects('insert')->with( @@ -132,7 +132,7 @@ public function testFirstOrCreateMethodRetrievesExistingRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true, []) + ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true) ->andReturn([[ 'id' => 456, 'parent_id' => 123, @@ -164,7 +164,7 @@ public function testFirstOrCreateMethodRetrievesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true, []) + ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true) ->andReturn([]); $sql = 'insert into "child_table" ("attr", "val", "parent_id", "updated_at", "created_at") values (?, ?, ?, ?, ?)'; @@ -177,7 +177,7 @@ public function testFirstOrCreateMethodRetrievesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], false, []) + ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], false) ->andReturn([[ 'id' => 456, 'parent_id' => 123, @@ -209,7 +209,7 @@ public function testUpdateOrCreateMethodCreatesNewRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true, []) + ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true) ->andReturn([]); $model->getConnection()->expects('insert')->with( @@ -239,7 +239,7 @@ public function testUpdateOrCreateMethodUpdatesExistingRecord(): void $model->getConnection() ->expects('select') - ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true, []) + ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true) ->andReturn([[ 'id' => 456, 'parent_id' => 123, @@ -276,7 +276,7 @@ public function testUpdateOrCreateMethodUpdatesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true, []) + ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], true) ->andReturn([]); $sql = 'insert into "child_table" ("attr", "val", "parent_id", "updated_at", "created_at") values (?, ?, ?, ?, ?)'; @@ -289,7 +289,7 @@ public function testUpdateOrCreateMethodUpdatesRecordCreatedJustNow(): void $model->getConnection() ->expects('select') - ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], false, []) + ->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', [123, 'foo'], false) ->andReturn([[ 'id' => 456, 'parent_id' => 123, diff --git a/tests/Database/DatabaseEloquentHasManyThroughCreateOrFirstTest.php b/tests/Database/DatabaseEloquentHasManyThroughCreateOrFirstTest.php index 62acbbfe9e87..b499100ef406 100644 --- a/tests/Database/DatabaseEloquentHasManyThroughCreateOrFirstTest.php +++ b/tests/Database/DatabaseEloquentHasManyThroughCreateOrFirstTest.php @@ -75,7 +75,6 @@ public function testCreateOrFirstMethodRetrievesExistingRecord(): void 'select "child".*, "pivot"."parent_id" as "laravel_through_key" from "child" inner join "pivot" on "pivot"."id" = "child"."pivot_id" where "pivot"."parent_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([[ 'id' => 789, @@ -115,7 +114,6 @@ public function testFirstOrCreateMethodCreatesNewRecord(): void 'select "child".*, "pivot"."parent_id" as "laravel_through_key" from "child" inner join "pivot" on "pivot"."id" = "child"."pivot_id" where "pivot"."parent_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([]); @@ -150,7 +148,6 @@ public function testFirstOrCreateMethodRetrievesExistingRecord(): void 'select "child".*, "pivot"."parent_id" as "laravel_through_key" from "child" inner join "pivot" on "pivot"."id" = "child"."pivot_id" where "pivot"."parent_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([[ 'id' => 789, @@ -190,7 +187,6 @@ public function testFirstOrCreateMethodRetrievesRecordCreatedJustNow(): void 'select "child".*, "pivot"."parent_id" as "laravel_through_key" from "child" inner join "pivot" on "pivot"."id" = "child"."pivot_id" where "pivot"."parent_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([]); @@ -208,7 +204,6 @@ public function testFirstOrCreateMethodRetrievesRecordCreatedJustNow(): void 'select "child".*, "pivot"."parent_id" as "laravel_through_key" from "child" inner join "pivot" on "pivot"."id" = "child"."pivot_id" where "pivot"."parent_id" = ? and ("attr" = ? and "val" = ?) limit 1', [123, 'foo', 'bar'], true, - [], ) ->andReturn([[ 'id' => 789, @@ -248,7 +243,6 @@ public function testUpdateOrCreateMethodCreatesNewRecord(): void 'select "child".*, "pivot"."parent_id" as "laravel_through_key" from "child" inner join "pivot" on "pivot"."id" = "child"."pivot_id" where "pivot"."parent_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([]); @@ -286,7 +280,6 @@ public function testUpdateOrCreateMethodUpdatesExistingRecord(): void 'select "child".*, "pivot"."parent_id" as "laravel_through_key" from "child" inner join "pivot" on "pivot"."id" = "child"."pivot_id" where "pivot"."parent_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([[ 'id' => 789, @@ -334,7 +327,6 @@ public function testUpdateOrCreateMethodUpdatesRecordCreatedJustNow(): void 'select "child".*, "pivot"."parent_id" as "laravel_through_key" from "child" inner join "pivot" on "pivot"."id" = "child"."pivot_id" where "pivot"."parent_id" = ? and ("attr" = ?) limit 1', [123, 'foo'], true, - [], ) ->andReturn([]); @@ -352,7 +344,6 @@ public function testUpdateOrCreateMethodUpdatesRecordCreatedJustNow(): void 'select "child".*, "pivot"."parent_id" as "laravel_through_key" from "child" inner join "pivot" on "pivot"."id" = "child"."pivot_id" where "pivot"."parent_id" = ? and ("attr" = ? and "val" = ?) limit 1', [123, 'foo', 'bar'], true, - [], ) ->andReturn([[ 'id' => 789, diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 3671b47e90aa..1773aff32783 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -82,12 +82,12 @@ public function testBasicSelectUseWritePdo() { $builder = $this->getMySqlBuilderWithProcessor(); $builder->getConnection()->shouldReceive('select')->once() - ->with('select * from `users`', [], false, []); + ->with('select * from `users`', [], false); $builder->useWritePdo()->select('*')->from('users')->get(); $builder = $this->getMySqlBuilderWithProcessor(); $builder->getConnection()->shouldReceive('select')->once() - ->with('select * from `users`', [], true, []); + ->with('select * from `users`', [], true); $builder->select('*')->from('users')->get(); } @@ -1747,31 +1747,31 @@ public function testUnionAggregate() { $expected = 'select count(*) as aggregate from ((select * from `posts`) union (select * from `videos`)) as `temp_table`'; $builder = $this->getMySqlBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true, []); + $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true); $builder->getProcessor()->shouldReceive('processSelect')->once(); $builder->from('posts')->union($this->getMySqlBuilder()->from('videos'))->count(); $expected = 'select count(*) as aggregate from ((select `id` from `posts`) union (select `id` from `videos`)) as `temp_table`'; $builder = $this->getMySqlBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true, []); + $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true); $builder->getProcessor()->shouldReceive('processSelect')->once(); $builder->from('posts')->select('id')->union($this->getMySqlBuilder()->from('videos')->select('id'))->count(); $expected = 'select count(*) as aggregate from ((select * from "posts") union (select * from "videos")) as "temp_table"'; $builder = $this->getPostgresBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true, []); + $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true); $builder->getProcessor()->shouldReceive('processSelect')->once(); $builder->from('posts')->union($this->getPostgresBuilder()->from('videos'))->count(); $expected = 'select count(*) as aggregate from (select * from (select * from "posts") union select * from (select * from "videos")) as "temp_table"'; $builder = $this->getSQLiteBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true, []); + $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true); $builder->getProcessor()->shouldReceive('processSelect')->once(); $builder->from('posts')->union($this->getSQLiteBuilder()->from('videos'))->count(); $expected = 'select count(*) as aggregate from (select * from (select * from [posts]) as [temp_table] union select * from (select * from [videos]) as [temp_table]) as [temp_table]'; $builder = $this->getSqlServerBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true, []); + $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true); $builder->getProcessor()->shouldReceive('processSelect')->once(); $builder->from('posts')->union($this->getSqlServerBuilder()->from('videos'))->count(); } @@ -1781,7 +1781,7 @@ public function testHavingAggregate() $expected = 'select count(*) as aggregate from (select (select `count(*)` from `videos` where `posts`.`id` = `videos`.`post_id`) as `videos_count` from `posts` having `videos_count` > ?) as `temp_table`'; $builder = $this->getMySqlBuilder(); $builder->getConnection()->shouldReceive('getDatabaseName'); - $builder->getConnection()->shouldReceive('select')->once()->with($expected, [0 => 1], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with($expected, [0 => 1], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -2263,7 +2263,7 @@ public function testHavingFollowedBySelectGet() { $builder = $this->getBuilder(); $query = 'select "category", count(*) as "total" from "item" where "department" = ? group by "category" having "total" > ?'; - $builder->getConnection()->shouldReceive('select')->once()->with($query, ['popular', 3], true, [])->andReturn([['category' => 'rock', 'total' => 5]]); + $builder->getConnection()->shouldReceive('select')->once()->with($query, ['popular', 3], true)->andReturn([['category' => 'rock', 'total' => 5]]); $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) { return $results; }); @@ -2274,7 +2274,7 @@ public function testHavingFollowedBySelectGet() // Using \Raw value $builder = $this->getBuilder(); $query = 'select "category", count(*) as "total" from "item" where "department" = ? group by "category" having "total" > 3'; - $builder->getConnection()->shouldReceive('select')->once()->with($query, ['popular'], true, [])->andReturn([['category' => 'rock', 'total' => 5]]); + $builder->getConnection()->shouldReceive('select')->once()->with($query, ['popular'], true)->andReturn([['category' => 'rock', 'total' => 5]]); $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) { return $results; }); @@ -2367,7 +2367,7 @@ public function testGetCountForPaginationWithBindings() $q->select('body')->from('posts')->where('id', 4); }, 'post'); - $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -2383,7 +2383,7 @@ public function testGetCountForPaginationWithColumnAliases() $columns = ['body as post_body', 'teaser', 'posts.created as published']; $builder->from('posts')->select($columns); - $builder->getConnection()->shouldReceive('select')->once()->with('select count("body", "teaser", "posts"."created") as aggregate from "posts"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count("body", "teaser", "posts"."created") as aggregate from "posts"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -2397,7 +2397,7 @@ public function testGetCountForPaginationWithUnion() $builder = $this->getBuilder(); $builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id')); - $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -2411,7 +2411,7 @@ public function testGetCountForPaginationWithUnionOrders() $builder = $this->getBuilder(); $builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'))->latest(); - $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -2425,7 +2425,7 @@ public function testGetCountForPaginationWithUnionLimitAndOffset() $builder = $this->getBuilder(); $builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'))->take(15)->skip(1); - $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3412,7 +3412,7 @@ public function testRawExpressionsInSelect() public function testFindReturnsFirstResultByID() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true, [])->andReturn([['foo' => 'bar']]); + $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([['foo' => 'bar']]); $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) { return $results; }); @@ -3436,7 +3436,7 @@ public function testFindOrReturnsFirstResultByID() public function testFirstMethodReturnsFirstResult() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true, [])->andReturn([['foo' => 'bar']]); + $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([['foo' => 'bar']]); $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) { return $results; }); @@ -3447,7 +3447,7 @@ public function testFirstMethodReturnsFirstResult() public function testFirstOrFailMethodReturnsFirstResult() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true, [])->andReturn([['foo' => 'bar']]); + $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([['foo' => 'bar']]); $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) { return $results; }); @@ -3458,7 +3458,7 @@ public function testFirstOrFailMethodReturnsFirstResult() public function testFirstOrFailMethodThrowsRecordNotFoundException() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true, [])->andReturn([]); + $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([]); $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [])->andReturn([]); @@ -3490,7 +3490,7 @@ public function testPluckMethodGetsCollectionOfColumnValues() public function testPluckAvoidsDuplicateColumnSelection() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select "foo" from "users" where "id" = ?', [1], true, [])->andReturn([['foo' => 'bar']]); + $builder->getConnection()->shouldReceive('select')->once()->with('select "foo" from "users" where "id" = ?', [1], true)->andReturn([['foo' => 'bar']]); $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) { return $results; }); @@ -3522,7 +3522,7 @@ public function testImplode() public function testValueMethodReturnsSingleColumn() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select "foo" from "users" where "id" = ? limit 1', [1], true, [])->andReturn([['foo' => 'bar']]); + $builder->getConnection()->shouldReceive('select')->once()->with('select "foo" from "users" where "id" = ? limit 1', [1], true)->andReturn([['foo' => 'bar']]); $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturn([['foo' => 'bar']]); $results = $builder->from('users')->where('id', '=', 1)->value('foo'); $this->assertSame('bar', $results); @@ -3531,7 +3531,7 @@ public function testValueMethodReturnsSingleColumn() public function testRawValueMethodReturnsSingleColumn() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select UPPER("foo") from "users" where "id" = ? limit 1', [1], true, [])->andReturn([['UPPER("foo")' => 'BAR']]); + $builder->getConnection()->shouldReceive('select')->once()->with('select UPPER("foo") from "users" where "id" = ? limit 1', [1], true)->andReturn([['UPPER("foo")' => 'BAR']]); $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['UPPER("foo")' => 'BAR']])->andReturn([['UPPER("foo")' => 'BAR']]); $results = $builder->from('users')->where('id', '=', 1)->rawValue('UPPER("foo")'); $this->assertSame('BAR', $results); @@ -3540,7 +3540,7 @@ public function testRawValueMethodReturnsSingleColumn() public function testAggregateFunctions() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3558,7 +3558,7 @@ public function testAggregateFunctions() $this->assertTrue($results); $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select max("id") as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select max("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3566,7 +3566,7 @@ public function testAggregateFunctions() $this->assertEquals(1, $results); $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select min("id") as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select min("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3574,7 +3574,7 @@ public function testAggregateFunctions() $this->assertEquals(1, $results); $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select sum("id") as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select sum("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3582,7 +3582,7 @@ public function testAggregateFunctions() $this->assertEquals(1, $results); $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select avg("id") as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select avg("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3590,7 +3590,7 @@ public function testAggregateFunctions() $this->assertEquals(1, $results); $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select avg("id") as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select avg("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3641,9 +3641,9 @@ public function testDoesntExistsOr() public function testAggregateResetFollowedByGet() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); - $builder->getConnection()->shouldReceive('select')->once()->with('select sum("id") as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 2]]); - $builder->getConnection()->shouldReceive('select')->once()->with('select "column1", "column2" from "users"', [], true, [])->andReturn([['column1' => 'foo', 'column2' => 'bar']]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select sum("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 2]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select "column1", "column2" from "users"', [], true)->andReturn([['column1' => 'foo', 'column2' => 'bar']]); $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3659,8 +3659,8 @@ public function testAggregateResetFollowedByGet() public function testAggregateResetFollowedBySelectGet() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select count("column1") as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); - $builder->getConnection()->shouldReceive('select')->once()->with('select "column2", "column3" from "users"', [], true, [])->andReturn([['column2' => 'foo', 'column3' => 'bar']]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count("column1") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select "column2", "column3" from "users"', [], true)->andReturn([['column2' => 'foo', 'column3' => 'bar']]); $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3674,8 +3674,8 @@ public function testAggregateResetFollowedBySelectGet() public function testAggregateResetFollowedByGetWithColumns() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select count("column1") as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); - $builder->getConnection()->shouldReceive('select')->once()->with('select "column2", "column3" from "users"', [], true, [])->andReturn([['column2' => 'foo', 'column3' => 'bar']]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count("column1") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select "column2", "column3" from "users"', [], true)->andReturn([['column2' => 'foo', 'column3' => 'bar']]); $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) { return $results; }); @@ -3689,7 +3689,7 @@ public function testAggregateResetFollowedByGetWithColumns() public function testAggregateWithSubSelect() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true, [])->andReturn([['aggregate' => 1]]); + $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); @@ -5171,12 +5171,12 @@ public function testSelectWithLockUsesWritePdo() { $builder = $this->getMySqlBuilderWithProcessor(); $builder->getConnection()->shouldReceive('select')->once() - ->with(m::any(), m::any(), false, []); + ->with(m::any(), m::any(), false); $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock()->get(); $builder = $this->getMySqlBuilderWithProcessor(); $builder->getConnection()->shouldReceive('select')->once() - ->with(m::any(), m::any(), false, []); + ->with(m::any(), m::any(), false); $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock(false)->get(); } diff --git a/tests/Integration/Database/QueryBuilderTest.php b/tests/Integration/Database/QueryBuilderTest.php index 4e964149659a..c323ff0e1513 100644 --- a/tests/Integration/Database/QueryBuilderTest.php +++ b/tests/Integration/Database/QueryBuilderTest.php @@ -11,7 +11,6 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Testing\Assert as PHPUnit; use Orchestra\Testbench\Attributes\DefineEnvironment; -use PDO; use PDOException; class QueryBuilderTest extends DatabaseTestCase @@ -601,12 +600,6 @@ public function testChunkMap() public function testPluck() { - // Test empty result set. - $this->assertSame( - [], - DB::table('posts')->whereRaw('0=1')->pluck('title')->toArray() - ); - // Test SELECT override, since pluck will take the first column. $this->assertSame([ 'Foo Post', @@ -643,47 +636,6 @@ public function testPluck() ], DB::table('posts')->pluck('title', 'content')->toArray()); } - public function testFetchUsing() - { - // Fetch column as a list. - $this->assertSame([ - 'Foo Post', - 'Bar Post', - ], DB::table('posts')->select(['title'])->fetchUsing(PDO::FETCH_COLUMN)->get()->toArray()); - - // Fetch the second column as a list (zero-indexed). - $this->assertSame([ - 'Lorem Ipsum.', - 'Lorem Ipsum.', - ], DB::table('posts')->select(['title', 'content'])->fetchUsing(PDO::FETCH_COLUMN, 1)->get()->toArray()); - - // Fetch two columns as key value pairs. - $this->assertSame([ - 1 => 'Foo Post', - 2 => 'Bar Post', - ], DB::table('posts')->select(['id', 'title'])->fetchUsing(PDO::FETCH_KEY_PAIR)->get()->toArray()); - - // Fetch data as associative array with custom key. - $result = DB::table('posts')->select(['id', 'title'])->fetchUsing(PDO::FETCH_UNIQUE)->get()->toArray(); - // Note: results are keyed by their post id here. - $this->assertSame('Foo Post', $result[1]->title); - $this->assertSame('Bar Post', $result[2]->title); - - // Use a cursor. - $this->assertSame([ - 'Foo Post', - 'Bar Post', - ], DB::table('posts')->select(['title'])->fetchUsing(PDO::FETCH_COLUMN)->cursor()->collect()->toArray()); - - // Test the default 'object' fetch mode. - $result = DB::table('posts')->select(['title'])->fetchUsing(PDO::FETCH_OBJ)->get()->toArray(); - $result2 = DB::table('posts')->select(['title'])->fetchUsing()->get()->toArray(); - $this->assertSame('Foo Post', $result[0]->title); - $this->assertSame('Bar Post', $result[1]->title); - $this->assertSame('Foo Post', $result2[0]->title); - $this->assertSame('Bar Post', $result2[1]->title); - } - protected function defineEnvironmentWouldThrowsPDOException($app) { $this->afterApplicationCreated(function () {