Skip to content

Commit

Permalink
[6.x] Fix plucking column name containing a space
Browse files Browse the repository at this point in the history
  • Loading branch information
shadoWalker89 committed Jan 30, 2020
1 parent fc844ce commit 62d6d99
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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($column, ' as ') !== false ? ' as ' : '\.';

return last(preg_split('~'.$seperator.'~', $column));
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -443,6 +450,18 @@ public function testPluckWithJoin()
$this->assertEquals(['[email protected]' => 'First post', '[email protected]' => 'Second post'], $query->pluck('posts.name', 'users.email as user_email')->all());
}

public function testPluckWithColumnNameContainingASpace()
{
EloquentTestUserWithSpaceInColumnName::create(['id' => 1, 'email address' => '[email protected]']);
EloquentTestUserWithSpaceInColumnName::create(['id' => 2, 'email address' => '[email protected]']);

$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(['[email protected]', '[email protected]'], $simple);
$this->assertEquals([1 => '[email protected]', 2 => '[email protected]'], $keyed);
}

public function testFindOrFail()
{
EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 62d6d99

Please sign in to comment.