Skip to content

Commit be784a8

Browse files
committed
fix: fix: fixing belongs search with custom foreign key
2 parents 3e5b106 + 93979c5 commit be784a8

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

src/Fields/EagerField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function getRelatedKey(
123123
Repository $repository
124124
): string {
125125
return $repository->resource->qualifyColumn(
126-
$this->getRelation($repository)->getRelated()->getForeignKey()
126+
$repository->resource->{$this->relation}()->getForeignKeyName()
127127
);
128128
}
129129

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/Feature/RepositorySearchServiceTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,49 @@ public function test_can_search_using_belongs_to_field(): void
7676
->assertJsonCount(2, 'data');
7777
}
7878

79+
public function test_can_search_using_belongs_to_field_with_custom_foreign_key(): void
80+
{
81+
$foreignUser = User::factory()->create([
82+
'name' => 'Curtis Dog',
83+
]);
84+
85+
Post::factory(4)->create([
86+
'edited_by' => $foreignUser->id,
87+
]);
88+
89+
$john = User::factory()->create([
90+
'name' => 'John Doe',
91+
]);
92+
93+
Post::factory(2)->create([
94+
'edited_by' => $john->id,
95+
]);
96+
97+
PostRepository::$related = [
98+
'editor' => BelongsTo::make('editor', UserRepository::class)->searchable([
99+
'users.name',
100+
]),
101+
];
102+
103+
$this->withoutExceptionHandling();
104+
$this->getJson(PostRepository::route(query: ['search' => 'John']))
105+
->assertJsonCount(2, 'data');
106+
}
107+
108+
public function test_can_match_closure(): void
109+
{
110+
User::factory(4)->create();
111+
112+
UserRepository::$match = [
113+
'is_active' => function ($request, $query) {
114+
$this->assertInstanceOf(Request::class, $request);
115+
$this->assertInstanceOf(Builder::class, $query);
116+
},
117+
];
118+
119+
$this->getJson('users?is_active=true');
120+
}
121+
79122
public function test_can_match_custom_matcher(): void
80123
{
81124
User::factory(1)->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
@@ -15,6 +15,7 @@
1515
*
1616
* @property mixed $id
1717
* @property mixed $user_id
18+
* @property mixed $edited_by
1819
* @property mixed $image
1920
* @property string $title
2021
* @property string $description
@@ -28,6 +29,7 @@ class Post extends Model
2829

2930
protected $fillable = [
3031
'id',
32+
'edited_by',
3133
'user_id',
3234
'image',
3335
'title',
@@ -45,6 +47,11 @@ public function user(): BelongsTo
4547
return $this->belongsTo(User::class);
4648
}
4749

50+
public function editor(): BelongsTo
51+
{
52+
return $this->belongsTo(User::class, 'edited_by');
53+
}
54+
4855
public function comments(): HasMany
4956
{
5057
return $this->hasMany(Comment::class);

tests/database/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)