Skip to content

Commit 93979c5

Browse files
authored
fix: fixing belongs search with custom foreign key (#512)
* fix: fixing belongs search with custom foreign key * Fix styling Co-authored-by: binaryk <[email protected]>
1 parent 98d7e9e commit 93979c5

File tree

7 files changed

+41
-1
lines changed

7 files changed

+41
-1
lines changed

src/Fields/EagerField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function getRelatedKey(
9898
Repository $repository
9999
): string {
100100
return $repository->resource->qualifyColumn(
101-
$this->getRelation($repository)->getRelated()->getForeignKey()
101+
$repository->resource->{$this->relation}()->getForeignKeyName()
102102
);
103103
}
104104

src/Getters/Getter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
use JsonSerializable;
2020
use ReturnTypeWillChange;
2121
use Symfony\Component\HttpFoundation\Response;
22+
2223
use function tap;
2324
use function throw_unless;
25+
2426
use Throwable;
2527

2628
/**

tests/Controllers/RepositoryAttachControllerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Database\Eloquent\Relations\Pivot;
1414
use Illuminate\Http\Request;
1515
use Illuminate\Support\Facades\Gate;
16+
1617
use function PHPUnit\Framework\assertInstanceOf;
1718

1819
class RepositoryAttachControllerTest extends IntegrationTest

tests/Feature/RepositorySearchServiceTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,34 @@ public function test_can_search_using_belongs_to_field(): void
228228
->assertJsonCount(2, 'data');
229229
}
230230

231+
public function test_can_search_using_belongs_to_field_with_custom_foreign_key(): void
232+
{
233+
$foreignUser = User::factory()->create([
234+
'name' => 'Curtis Dog',
235+
]);
236+
237+
Post::factory(4)->create([
238+
'edited_by' => $foreignUser->id,
239+
]);
240+
241+
$john = User::factory()->create([
242+
'name' => 'John Doe',
243+
]);
244+
245+
Post::factory(2)->create([
246+
'edited_by' => $john->id,
247+
]);
248+
249+
PostRepository::$related = [
250+
'editor' => BelongsTo::make('editor', UserRepository::class)->searchable([
251+
'users.name',
252+
]),
253+
];
254+
255+
$this->getJson('posts?search=John')
256+
->assertJsonCount(2, 'data');
257+
}
258+
231259
public function test_can_order_using_filter_sortable_definition(): void
232260
{
233261
User::factory()->create([

tests/Fields/FieldTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Binaryk\LaravelRestify\Tests\IntegrationTest;
1111
use Illuminate\Database\Eloquent\Model;
1212
use Illuminate\Routing\Route;
13+
1314
use function PHPUnit\Framework\assertInstanceOf;
1415
use function PHPUnit\Framework\assertSame;
1516

tests/Fixtures/Post/Post.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Class Post.
1313
* @property mixed $id
1414
* @property mixed $user_id
15+
* @property mixed $edited_by
1516
* @property mixed $image
1617
* @property mixed $title
1718
* @property mixed $description
@@ -25,6 +26,7 @@ class Post extends Model
2526

2627
protected $fillable = [
2728
'id',
29+
'edited_by',
2830
'user_id',
2931
'image',
3032
'title',
@@ -37,4 +39,9 @@ public function user(): BelongsTo
3739
{
3840
return $this->belongsTo(User::class);
3941
}
42+
43+
public function editor(): BelongsTo
44+
{
45+
return $this->belongsTo(User::class, 'edited_by');
46+
}
4047
}

tests/Migrations/2019_12_22_000005_create_posts_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function up()
2121
$table->string('image')->nullable();
2222
$table->string('category')->nullable();
2323
$table->boolean('is_active')->default(true);
24+
$table->unsignedInteger('edited_by')->index()->nullable();
2425
$table->timestamps();
2526
});
2627
}

0 commit comments

Comments
 (0)