Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Fix plucking column name containing a space #31299

Merged
merged 2 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(strtolower($column), ' as ') !== false ? ' as ' : '\.';

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

/**
Expand Down
26 changes: 25 additions & 1 deletion 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 @@ -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(['[email protected]' => 'First post', '[email protected]' => 'Second post'], $query->pluck('posts.name', 'users.email as user_email')->all());
$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()
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