Skip to content

Commit 1f7f3cf

Browse files
authored
Parser to use table join (#81)
* Parser to use table join * Update BaseRepository.php * check if table already has . notation
1 parent 03a84d3 commit 1f7f3cf

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Contracts/Parser.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function parseJoinSorts(Collection $sorts)
130130

131131
foreach ($sorts as $sortF => $sortD) {
132132
[$with, $key] = explode('.', $sortF);
133-
$relation = self::$model->{$with}();
133+
$relation = self::$model->{Helpers::camel($with)}();
134134
$type = class_basename(get_class($relation));
135135

136136
if ($type === 'HasOne') {
@@ -143,10 +143,15 @@ protected function parseJoinSorts(Collection $sorts)
143143
continue;
144144
}
145145

146+
$withConnection = $relation->getRelated()->getConnection()->getDatabaseName();
147+
146148
$withTable = $relation->getRelated()->getTable();
147149

148-
$this->repository->join($withTable, "{$withTable}.{$foreignKey}", "{$currentTable}.{$localKey}");
149-
$this->repository->orderBy("{$withTable}.{$key}", $sortD);
150+
$withTableName = strpos($withTable, ".") === false ? $withConnection . '.' . $withTable : $withTable;
151+
152+
$this->repository->join($withTableName, "{$withTableName}.{$foreignKey}", "{$currentTable}.{$localKey}");
153+
$this->repository->orderBy("{$withTableName}.{$key}", $sortD);
154+
150155
}
151156
}
152157

src/Repository/BaseRepository.php

+6
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ public function get(array $columns = ['*'])
254254
return $models;
255255
}
256256

257+
public function dd(array $columns = ['*'])
258+
{
259+
$this->newQuery()->eagerLoad()->setClauses()->setScopes();
260+
$this->query->dd($columns);
261+
}
262+
257263
public function getRaw(array $columns = ['*'])
258264
{
259265
$this->newQuery()->eagerLoad()->setClauses()->setScopes();

0 commit comments

Comments
 (0)