Skip to content

Commit a2ba04a

Browse files
committed
Added the ability to pass the alias as a string in the second parameter
1 parent 4837a42 commit a2ba04a

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,21 @@ Sometimes, you are going to need to use table aliases on your joins because you
150150
Post::joinRelationshipUsingAlias('category.parent')->get();
151151
```
152152

153-
Or, you can also call the `as` function inside the join callback.
153+
In case you need to specify the name of the alias which is going to be used, you can do in two different ways:
154+
155+
1. Passing a string as the second parameter (this won't work for nested joins):
156+
157+
```php
158+
Post::joinRelationshipUsingAlias('category', 'category_alias')->get();
159+
```
160+
161+
2. Calling the `as` function inside the join callback.
154162

155163
```php
156164
Post::joinRelationship('category.parent', [
165+
'category' => function ($join) {
166+
$join->as('category_alias');
167+
},
157168
'parent' => function ($join) {
158169
$join->as('category_parent');
159170
},

src/PowerJoins.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ trait PowerJoins
4040
public function scopeJoinRelationship(Builder $query, $relationName, $callback = null, $joinType = 'join', $useAlias = false, bool $disableExtraConditions = false): void
4141
{
4242
$joinType = PowerJoins::$joinMethodsMap[$joinType] ?? $joinType;
43+
$callback = $this->formatJoinCallback($relationName, $callback);
4344

4445
if (is_null($query->getSelect())) {
4546
$query->select(sprintf('%s.*', $query->getModel()->getTable()));
@@ -385,4 +386,22 @@ public function clearPowerJoinCaches()
385386

386387
return $this;
387388
}
389+
390+
/**
391+
* Format the join callback.
392+
*
393+
* @param string $relationName
394+
* @param mixed $callback
395+
* @return mixed
396+
*/
397+
protected function formatJoinCallback(string $relationName, $callback)
398+
{
399+
if (is_string($callback)) {
400+
return function ($join) use ($callback) {
401+
$join->as($callback);
402+
};
403+
}
404+
405+
return $callback;
406+
}
388407
}

tests/JoinRelationshipTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,28 @@ public function test_join_model_with_soft_deletes()
436436
);
437437
}
438438

439+
/** @test */
440+
public function test_join_model_has_one_with_alias_and_select()
441+
{
442+
$profile = factory(UserProfile::class)->create();
443+
444+
$user = User::query()->select('profile.city')->leftJoinRelationship('profile', function ($join) {
445+
$join->as('profile');
446+
})->first();
447+
448+
$this->assertEquals($profile->city, $user->city);
449+
}
450+
451+
/** @test */
452+
public function test_join_with_alias_using_alias_as_string()
453+
{
454+
$innerJoinQuery = User::query()->select('profile.city')->joinRelationship('profile', 'p')->toSql();
455+
$leftJoinQuery = User::query()->select('profile.city')->leftJoinRelationship('profile', 'p')->toSql();
456+
457+
$this->assertStringContainsString('inner join "user_profiles" as "p"', $innerJoinQuery);
458+
$this->assertStringContainsString('left join "user_profiles" as "p"', $leftJoinQuery);
459+
}
460+
439461
/** @test */
440462
public function test_it_automatically_includes_select_statement_if_not_defined()
441463
{

0 commit comments

Comments
 (0)