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

[8.x] Add qualifyColumns method to Model class #38403

Merged
merged 18 commits into from
Aug 17, 2021
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,17 @@ public function qualifyColumn($column)
return $this->model->qualifyColumn($column);
}

/**
* Qualify the column's lists name by the model's table.
*
* @param array|\Illuminate\Database\Query\Expression $columns
* @return array
*/
public function qualifyColumns($columns)
{
return $this->model->qualifyColumns($columns);
}

/**
* Get the given macro by name.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,23 @@ public function qualifyColumn($column)
return $this->getTable().'.'.$column;
}

/**
* Qualify the column's lists name by the model's table.
*
* @param array $columns
* @return array
*/
public function qualifyColumns($columns)
{
$qualifiedArray = [];

foreach ($columns as $column) {
$qualifiedArray[] = $this->qualifyColumn($column);
}

return $qualifiedArray;
}

/**
* Create a new instance of the given model.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ public function testQualifyColumn()
$this->assertSame('stub.column', $builder->qualifyColumn('column'));
}

public function testQualifyColumns()
{
$builder = new Builder(m::mock(BaseBuilder::class));
$builder->shouldReceive('from')->with('stub');

$builder->setModel(new EloquentModelStub);

$this->assertEquals(['stub.column', 'stub.name'], $builder->qualifyColumns(['column', 'name']));
}

public function testGetMethodLoadsModelsAndHydratesEagerRelations()
{
$builder = m::mock(Builder::class.'[getModels,eagerLoadRelations]', [$this->getMockQueryBuilder()]);
Expand Down