diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 4f7d55b8655e..216c6c1d9021 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2310,7 +2310,13 @@ function () { */ protected function stripTableForPluck($column) { - return is_null($column) ? $column : last(preg_split('~\.| ~', $column)); + if (is_null($column)) { + return $column; + } + + $seperator = strpos(strtolower($column), ' as ') !== false ? ' as ' : '\.'; + + return last(preg_split('~'.$seperator.'~i', $column)); } /** diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index ac39258cb30a..492c9f885db8 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -69,6 +69,13 @@ protected function createSchema() $table->timestamps(); }); + $this->schema('default')->create('users_with_space_in_colum_name', function ($table) { + $table->increments('id'); + $table->string('name')->nullable(); + $table->string('email address'); + $table->timestamps(); + }); + foreach (['default', 'second_connection'] as $connection) { $this->schema($connection)->create('users', function ($table) { $table->increments('id'); @@ -440,7 +447,19 @@ public function testPluckWithJoin() $this->assertEquals([1 => 'First post', 2 => 'Second post'], $query->pluck('posts.name', 'posts.id')->all()); $this->assertEquals([2 => 'First post', 1 => 'Second post'], $query->pluck('posts.name', 'users.id')->all()); - $this->assertEquals(['abigailotwell@gmail.com' => 'First post', 'taylorotwell@gmail.com' => 'Second post'], $query->pluck('posts.name', 'users.email as user_email')->all()); + $this->assertEquals(['abigailotwell@gmail.com' => 'First post', 'taylorotwell@gmail.com' => 'Second post'], $query->pluck('posts.name', 'users.email AS user_email')->all()); + } + + public function testPluckWithColumnNameContainingASpace() + { + EloquentTestUserWithSpaceInColumnName::create(['id' => 1, 'email address' => 'taylorotwell@gmail.com']); + EloquentTestUserWithSpaceInColumnName::create(['id' => 2, 'email address' => 'abigailotwell@gmail.com']); + + $simple = EloquentTestUserWithSpaceInColumnName::oldest('id')->pluck('users_with_space_in_colum_name.email address')->all(); + $keyed = EloquentTestUserWithSpaceInColumnName::oldest('id')->pluck('email address', 'id')->all(); + + $this->assertEquals(['taylorotwell@gmail.com', 'abigailotwell@gmail.com'], $simple); + $this->assertEquals([1 => 'taylorotwell@gmail.com', 2 => 'abigailotwell@gmail.com'], $keyed); } public function testFindOrFail() @@ -1673,6 +1692,11 @@ public function friends() } } +class EloquentTestUserWithSpaceInColumnName extends EloquentTestUser +{ + protected $table = 'users_with_space_in_colum_name'; +} + class EloquentTestNonIncrementing extends Eloquent { protected $table = 'non_incrementing_users';