Skip to content

Commit

Permalink
Allow column aliases in query builder pluck
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Nov 15, 2015
1 parent 597a0af commit 4fd6db1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1562,14 +1562,14 @@ public function lists($column, $key = null)
}

/**
* Strip off the table name from a column identifier.
* Strip off the table name or alias from a column identifier.
*
* @param string $column
* @return string
*/
protected function stripTable($column)
{
return is_null($column) ? $column : last(explode('.', $column));
return is_null($column) ? $column : last(preg_split('~\.| ~', $column));
}

/**
Expand Down
10 changes: 6 additions & 4 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function setUp()
{
$this->schema()->create('users', function ($table) {
$table->increments('id');
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('role')->default('standard');
$table->timestamps();
Expand Down Expand Up @@ -181,16 +182,17 @@ public function testPluck()

public function testPluckWithJoin()
{
$user1 = EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
$user2 = EloquentTestUser::create(['id' => 2, 'email' => '[email protected]']);
$user1 = EloquentTestUser::create(['id' => 1, 'name' => 'Taylor', 'email' => '[email protected]']);
$user2 = EloquentTestUser::create(['id' => 2, 'name' => 'Abigail', 'email' => '[email protected]']);

$user2->posts()->create(['id' => 1, 'name' => 'First post']);
$user1->posts()->create(['id' => 2, 'name' => 'Second post']);

$query = EloquentTestUser::join('posts', 'users.id', '=', 'posts.user_id');

$this->assertEquals([1 => 'First post', 2 => 'Second post'], $query->pluck('name', 'posts.id')->all());
$this->assertEquals([2 => 'First post', 1 => 'Second post'], $query->pluck('name', 'users.id')->all());
$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(['Abigail' => 'First post', 'Taylor' => 'Second post'], $query->pluck('posts.name', 'users.name as user_name')->all());
}

public function testFindOrFail()
Expand Down

0 comments on commit 4fd6db1

Please sign in to comment.