Skip to content

Commit 9b5cb73

Browse files
authored
Merge pull request #33 from kirschbaum-development/issue-31
Fixed the ability to pass nested closures in join callbacks when using aliases
2 parents 067f6db + 0b7f801 commit 9b5cb73

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
All notable changes to `eloquent-power-joins` will be documented in this file.
44

5+
## 2.2.2 - 2020-10
6+
- Fixed the ability to pass nested closures in join callbacks when using aliases;
7+
58
## 2.2.1 - 2020-10
69
- Fixed nested conditions in relationship definitions;
710

src/PowerJoinClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function whereNull($columns, $boolean = 'and', $not = false)
115115

116116
public function where($column, $operator = null, $value = null, $boolean = 'and')
117117
{
118-
if ($this->alias && Str::contains($column, $this->tableName)) {
118+
if ($this->alias && is_string($column) && Str::contains($column, $this->tableName)) {
119119
$column = str_replace("{$this->tableName}.", "{$this->alias}.", $column);
120120
}
121121

tests/JoinRelationshipTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,27 @@ public function test_it_joins_different_tables_with_same_relationship_name()
397397
$query
398398
);
399399
}
400+
401+
/** @test */
402+
public function test_passing_where_closure_inside_join_callback()
403+
{
404+
$query = Post::query()
405+
->joinRelationship('category', function ($join) {
406+
$join->as('category_alias')
407+
->where(function ($query) {
408+
$query->whereNull('category_alias.parent_id')
409+
->orWhere('category_alias.parent_id', 3);
410+
});
411+
});
412+
413+
$sql = $query->toSql();
414+
415+
// executing to make sure it does not throw exceptions
416+
$query->get();
417+
418+
$this->assertStringContainsString(
419+
'inner join "categories" as "category_alias" on "posts"."category_id" = "category_alias"."id" and ("category_alias"."parent_id" is null or "category_alias"."parent_id" = ?)',
420+
$sql
421+
);
422+
}
400423
}

0 commit comments

Comments
 (0)