From 3c7a44beacf919fc106d203883c2e57f2e568b3a Mon Sep 17 00:00:00 2001 From: Blazik Date: Sun, 29 Oct 2023 21:09:10 +0100 Subject: [PATCH] fixes --- src/Models/Scopes/MappedColumnsScope.php | 34 ++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Models/Scopes/MappedColumnsScope.php b/src/Models/Scopes/MappedColumnsScope.php index 03990ee..3b3c94d 100644 --- a/src/Models/Scopes/MappedColumnsScope.php +++ b/src/Models/Scopes/MappedColumnsScope.php @@ -40,18 +40,36 @@ private function mapColumns($array, $mappedColToDbColWithTable) } foreach ($array as $key => $column) { - $columnName = is_array($column) ? $column['column'] : $column; + if (is_array($column)) { + if ($column['type'] === 'Exists') { + $column['query']->applyBeforeQueryCallbacks(); + + } elseif ($column['type'] === 'Column') { + if ($newColumnName = $mappedColToDbColWithTable[$column['first']] ?? null) { + $array[$key]['first'] = $newColumnName; + } + + if ($newColumnName = $mappedColToDbColWithTable[$column['second']] ?? null) { + $array[$key]['second'] = $newColumnName; + } + + } elseif (isset($column['column'])) { + if ($newColumnName = $mappedColToDbColWithTable[$column['column']] ?? null) { + $array[$key]['column'] = $newColumnName; + } + } + + // fixme: some unhandled cases? - if ($columnName === '*' || str_contains($columnName, ' as ')) { // todo: add support for column aliases continue; } - if ($newColumnName = $mappedColToDbColWithTable[$columnName] ?? null) { - if (is_array($column)) { - $array[$key]['column'] = $newColumnName; - } else { - $array[$key] = $newColumnName; - } + if ($column === '*' || str_contains($column, ' as ')) { // todo: add support for column aliases + continue; + } + + if ($newColumnName = $mappedColToDbColWithTable[$column] ?? null) { + $array[$key] = $newColumnName; } }