Skip to content

Commit 0351228

Browse files
committed
Throw exception if using unsupported callback
1 parent 3c1af9b commit 0351228

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Kirschbaum\PowerJoins\Exceptions;
4+
5+
use Exception;
6+
7+
class SingleCallbackNotAllowed extends Exception
8+
{
9+
/** The error message */
10+
protected $message = 'Single callback is not allowed here.';
11+
}

src/Mixins/RelationshipsExtraMethods.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Database\Eloquent\SoftDeletingScope;
1717
use Illuminate\Database\MySqlConnection;
1818
use Illuminate\Support\Str;
19+
use Kirschbaum\PowerJoins\Exceptions\SingleCallbackNotAllowed;
1920
use Kirschbaum\PowerJoins\PowerJoinClause;
2021
use Kirschbaum\PowerJoins\StaticCache;
2122

@@ -129,6 +130,8 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
129130

130131
if (is_array($callback) && isset($callback[$this->getTable()])) {
131132
$callback[$this->getTable()]($join);
133+
} elseif ($callback && is_callable($callback)) {
134+
throw new SingleCallbackNotAllowed();
132135
}
133136
});
134137

tests/JoinRelationshipTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kirschbaum\PowerJoins\Tests;
44

55
use Exception;
6+
use Kirschbaum\PowerJoins\Exceptions\SingleCallbackNotAllowed;
67
use Kirschbaum\PowerJoins\PowerJoinClause;
78
use Kirschbaum\PowerJoins\Tests\Models\Category;
89
use Kirschbaum\PowerJoins\Tests\Models\Comment;
@@ -271,6 +272,14 @@ public function test_join_belongs_to_many_with_callback()
271272
);
272273
}
273274

275+
/** @test */
276+
public function test_single_callback_in_belongs_to_many_should_throw_exception()
277+
{
278+
$this->expectException(SingleCallbackNotAllowed::class);
279+
280+
User::query()->joinRelationship('groups', fn ($join) => $join->where('groups.name', 'Test'));
281+
}
282+
274283
/** @test */
275284
public function test_it_doesnt_join_the_same_relationship_twice()
276285
{

0 commit comments

Comments
 (0)