Skip to content

Commit 9a97b55

Browse files
author
Deploy
committed
feat: Added lateralJoin support
1 parent b4e6e65 commit 9a97b55

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

src/Query/Concerns/BuildsJoins.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,28 @@ protected function newJoinClause(IlluminateQueryBuilder $parentQuery, $type, $ta
182182
return new JoinClause($parentQuery, $type, $table);
183183
}
184184

185+
/**
186+
* Add a lateral join clause to the query.
187+
*
188+
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<*>|string $query
189+
* @param string $as
190+
* @param string $type
191+
* @return $this
192+
*/
193+
public function joinLateral($query, string $as, string $type = 'inner')
194+
{
195+
assert($query instanceof Builder);
196+
197+
$query->importTableAliases($this);
198+
$query->importTableAliases([$as => $as]);
199+
$this->importTableAliases($query);
200+
201+
[$query] = $this->createSub($query);
202+
203+
$expression = $query . ' as ' . $this->grammar->wrapTable($as);
204+
205+
$this->joins[] = $this->newJoinLateralClause($this, $type, new Expression($expression));
206+
207+
return $this;
208+
}
185209
}

src/Query/Concerns/CompilesColumns.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ protected function normalizeColumnReferences(IlluminateQueryBuilder $query, stri
172172

173173
$references = explode('.', $column);
174174

175-
176175
$tableAlias = $query->getTableAlias($references[0]);
177176

178177
if (isset($tableAlias)) {
@@ -184,6 +183,7 @@ protected function normalizeColumnReferences(IlluminateQueryBuilder $query, stri
184183
array_unshift($references, $tableAlias);
185184
}
186185

186+
// geen tableAlias, table is parent...waarom geen tableAlias?
187187
if ($tableAlias === null && array_key_exists($table, $query->tableAliases)) {
188188
array_unshift($references, $query->tableAliases[$table]);
189189
}

src/Query/Concerns/CompilesJoins.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function extractTableAndAlias(Builder $query, $join): array
2929

3030
return [$table, $alias];
3131
}
32+
3233
}
3334

3435
$table = (string) $this->wrapTable($join->table);

tests/Query/JoinTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,44 @@
110110
'coordinate',
111111
]);
112112
});
113+
114+
test('joinLateral', function () {
115+
$controlledLocations = DB::table('locations')
116+
->whereColumn('locations.led_by', '==', 'characters.id')
117+
->limit(3);
118+
119+
$leadingLadies = DB::table('characters')
120+
->joinLateral(
121+
$controlledLocations,
122+
'controlled_territory',
123+
)
124+
->orderBy('name')
125+
->get();
126+
127+
expect($leadingLadies)->toHaveCount(6);
128+
});
129+
130+
131+
test('joinLateral with selected fields', function () {
132+
$controlledLocations = DB::table('locations')
133+
->select('id as location_id', 'name as location_name')
134+
->whereColumn('locations.led_by', '==', 'characters.id')
135+
->orderBy('name')
136+
->limit(3);
137+
138+
$leadingLadies = DB::table('characters')
139+
->select('id', 'name', 'controlled_territory.location_name as territory_name')
140+
->joinLateral(
141+
$controlledLocations,
142+
'controlled_territory',
143+
)
144+
->orderBy('name')
145+
->get();
146+
147+
expect($leadingLadies)->toHaveCount(6);
148+
149+
expect(($leadingLadies[1])->name)->toBe('Cersei');
150+
expect(($leadingLadies[2])->name)->toBe('Daenerys');
151+
expect(($leadingLadies[2])->territory_name)->toBe('Astapor');
152+
expect(($leadingLadies[5])->name)->toBe('Sansa');
153+
});

0 commit comments

Comments
 (0)