File tree Expand file tree Collapse file tree 7 files changed +41
-1
lines changed Expand file tree Collapse file tree 7 files changed +41
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1919use JsonSerializable ;
2020use ReturnTypeWillChange ;
2121use Symfony \Component \HttpFoundation \Response ;
22+
2223use function tap ;
2324use function throw_unless ;
25+
2426use Throwable ;
2527
2628/**
Original file line number Diff line number Diff line change 1313use Illuminate \Database \Eloquent \Relations \Pivot ;
1414use Illuminate \Http \Request ;
1515use Illuminate \Support \Facades \Gate ;
16+
1617use function PHPUnit \Framework \assertInstanceOf ;
1718
1819class RepositoryAttachControllerTest extends IntegrationTest
Original file line number Diff line number Diff 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 ([
Original file line number Diff line number Diff line change 1010use Binaryk \LaravelRestify \Tests \IntegrationTest ;
1111use Illuminate \Database \Eloquent \Model ;
1212use Illuminate \Routing \Route ;
13+
1314use function PHPUnit \Framework \assertInstanceOf ;
1415use function PHPUnit \Framework \assertSame ;
1516
Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments