Skip to content

Commit 63caeab

Browse files
committed
cleanup
1 parent 6b8519c commit 63caeab

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

src/Models/Scopes/MappedColumnsScope.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,22 @@ class MappedColumnsScope implements Scope
1414
public function apply(EloquentBuilder $builder, Model $model): void
1515
{
1616
$builder->beforeQuery(function (QueryBuilder $builder) use ($model) {
17-
if (!$mappedColToDbCol = $model->columns ?? null) {
17+
if (!$attributeToColumn = $model->mapAttributeToColumn ?? null) {
1818
return;
1919
}
2020

2121
$modelTable = $model->getTable();
22-
$mappedColToDbColWithTable = collect($mappedColToDbCol)
22+
$attributeToColumnWithTable = collect($attributeToColumn)
2323
->mapWithKeys(fn($value, $key) => [$modelTable . '.' . $key => $modelTable . '.' . $value])
24-
->merge($mappedColToDbCol);
24+
->merge($attributeToColumn);
2525

26-
$dbColToMappedCol = $mappedColToDbColWithTable->flip();
27-
28-
$builder->columns = $this->mapColumns(
29-
$builder->columns,
30-
$mappedColToDbColWithTable,
31-
$dbColToMappedCol,
32-
$model
33-
);
34-
35-
$builder->wheres = $this->mapColumns(
36-
$builder->wheres,
37-
$mappedColToDbColWithTable,
38-
$dbColToMappedCol,
39-
$model
40-
);
41-
42-
$builder->joins = $this->mapJoins($builder->joins, $mappedColToDbColWithTable);
26+
$builder->columns = $this->mapColumns($builder->columns, $attributeToColumnWithTable);
27+
$builder->wheres = $this->mapColumns($builder->wheres, $attributeToColumnWithTable);
28+
$builder->joins = $this->mapJoins($builder->joins, $attributeToColumnWithTable);
4329
});
4430
}
4531

46-
private function mapColumns($array, $mappedColToDbColWithTable, $dbColToMappedCol, $model)
32+
private function mapColumns($array, $mappedColToDbColWithTable)
4733
{
4834
if (!$array) {
4935
return $array;

src/Models/Traits/HasMappableColumns.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Illuminate\Support\Traits\Macroable;
99

1010
/**
11-
* @property array $columns
11+
* @property array $mapAttributeToColumn
1212
*/
1313
trait HasMappableColumns
1414
{
@@ -21,13 +21,13 @@ protected static function bootHasMappableColumns(): void
2121
{
2222
static::addGlobalScope(new MappedColumnsScope());
2323

24-
foreach ((new static)->columns ?? [] as $mappedCol => $dbCol) {
25-
static::macro('get' . Str::studly($mappedCol) . 'Attribute', function () use ($dbCol) {
26-
return $this->$dbCol;
24+
foreach ((new static)->mapAttributeToColumn ?? [] as $attribute => $column) {
25+
static::macro('get' . Str::studly($attribute) . 'Attribute', function () use ($column) {
26+
return $this->$column;
2727
});
2828

29-
static::macro('set' . Str::studly($mappedCol) . 'Attribute', function ($value) use ($dbCol) {
30-
$this->$dbCol = $value;
29+
static::macro('set' . Str::studly($attribute) . 'Attribute', function ($value) use ($column) {
30+
$this->$column = $value;
3131
});
3232
}
3333
}
@@ -37,16 +37,16 @@ public function newInstance($attributes = [], $exists = false): Model
3737
/** @var Model $instance */
3838
$instance = parent::newInstance($attributes, $exists);
3939

40-
foreach ($instance->columns ?? [] as $mappedCol => $dbCol) {
41-
$instance->hidden[] = $dbCol;
42-
$instance->appends[] = $mappedCol;
40+
foreach ($instance->mapAttributeToColumn ?? [] as $attribute => $column) {
41+
$instance->hidden[] = $column;
42+
$instance->appends[] = $attribute;
4343

44-
if (in_array($mappedCol, $instance->guarded)) {
45-
$instance->guarded[] = $dbCol;
44+
if (in_array($attribute, $instance->guarded)) {
45+
$instance->guarded[] = $column;
4646
}
4747

48-
if (in_array($mappedCol, $instance->fillable)) {
49-
$instance->fillable[] = $dbCol;
48+
if (in_array($attribute, $instance->fillable)) {
49+
$instance->fillable[] = $column;
5050
}
5151
}
5252

0 commit comments

Comments
 (0)