Skip to content

Commit

Permalink
Added the ability to call methods from custom eloquent builders from …
Browse files Browse the repository at this point in the history
…the joins
  • Loading branch information
luisdalmolin committed Mar 26, 2024
1 parent 4b948b1 commit fc8bb37
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/PowerJoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,14 @@ public function __call($name, $arguments)
} else {
if (static::hasMacro($name)) {
return $this->macroCall($name, $arguments);
} else {
throw new InvalidArgumentException(sprintf('Method %s does not exist in PowerJoinClause class', $name));
}

$eloquentBuilder = $this->getModel()->newEloquentBuilder($this);
if (method_exists($eloquentBuilder, $name)) {
return $eloquentBuilder->{$name}(...$arguments);
}

throw new InvalidArgumentException(sprintf('Method %s does not exist in PowerJoinClause class', $name));
}
}
}
24 changes: 24 additions & 0 deletions tests/JoinRelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,30 @@ public function test_apply_condition_to_join_using_related_model_scopes()
);
}

/** @test */
public function test_apply_condition_to_join_using_custom_eloquent_builder_model_method()
{
$queryBuilder = User::query()->joinRelationship('posts', function ($join) {
// whereReviewed() is an method of the custom in the PostBuilder builder in the Post model
$join->whereReviewed();
});

$query = $queryBuilder->toSql();

// running to make sure it doesn't throw any exceptions
$queryBuilder->get();

$this->assertStringContainsString(
'inner join "posts" on "posts"."user_id" = "users"."id"',
$query
);

$this->assertStringContainsString(
'and "reviewed" = ?',
$query
);
}

/** @test */
public function test_apply_condition_to_nested_joins()
{
Expand Down
13 changes: 13 additions & 0 deletions tests/Models/Builder/PostBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Kirschbaum\PowerJoins\Tests\Models\Builder;

use Illuminate\Database\Eloquent\Builder;

class PostBuilder extends Builder
{
public function whereReviewed(): self
{
return $this->where('reviewed', true);
}
}
8 changes: 7 additions & 1 deletion tests/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use Kirschbaum\PowerJoins\PowerJoins;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kirschbaum\PowerJoins\Tests\Models\Builder\PostBuilder;

class Post extends Model
{
Expand Down Expand Up @@ -83,4 +84,9 @@ public function translations(): HasMany
{
return $this->hasMany(PostTranslation::class);
}

public function newEloquentBuilder($query): PostBuilder
{
return new PostBuilder($query);
}
}

0 comments on commit fc8bb37

Please sign in to comment.