2
2
3
3
namespace Illuminate \Tests \Integration \Routing ;
4
4
5
+ use Illuminate \Database \Eloquent \Concerns \HasUlids ;
5
6
use Illuminate \Database \Eloquent \Concerns \HasUuids ;
6
7
use Illuminate \Database \Eloquent \Model ;
7
8
use Illuminate \Database \Eloquent \SoftDeletes ;
@@ -41,6 +42,13 @@ protected function defineDatabaseMigrations(): void
41
42
$ table ->timestamps ();
42
43
});
43
44
45
+ Schema::create ('tags ' , function (Blueprint $ table ) {
46
+ $ table ->ulid ('id ' )->primary ();
47
+ $ table ->string ('slug ' );
48
+ $ table ->integer ('post_id ' );
49
+ $ table ->timestamps ();
50
+ });
51
+
44
52
Schema::create ('comments ' , function (Blueprint $ table ) {
45
53
$ table ->uuid ('id ' )->primary ();
46
54
$ table ->string ('slug ' );
@@ -51,6 +59,7 @@ protected function defineDatabaseMigrations(): void
51
59
$ this ->beforeApplicationDestroyed (function () {
52
60
Schema::dropIfExists ('users ' );
53
61
Schema::dropIfExists ('posts ' );
62
+ Schema::dropIfExists ('tags ' );
54
63
Schema::dropIfExists ('comments ' );
55
64
});
56
65
}
@@ -261,6 +270,37 @@ public function testImplicitRouteBindingChildHasUuids()
261
270
$ response = $ this ->getJson ("/user/ {$ user ->id }/comment-slug/ {$ comment ->slug }" );
262
271
$ response ->assertJsonFragment (['id ' => $ comment ->id ]);
263
272
}
273
+
274
+ public function testImplicitRouteBindingChildHasUlids ()
275
+ {
276
+ $ user = ImplicitBindingUser::create (['name ' => 'Michael Nabil ' ]);
277
+ $ post = ImplicitBindingPost::create (['user_id ' => $ user ->id ]);
278
+ $ tag = ImplicitBindingTag::create ([
279
+ 'slug ' => 'slug ' ,
280
+ 'post_id ' => $ post ->id ,
281
+ ]);
282
+
283
+ config (['app.key ' => str_repeat ('a ' , 32 )]);
284
+
285
+ $ function = function (ImplicitBindingPost $ post , ImplicitBindingTag $ tag ) {
286
+ return [$ post , $ tag ];
287
+ };
288
+
289
+ Route::middleware (['web ' ])->group (function () use ($ function ) {
290
+ Route::get ('/post/{post}/tag/{tag} ' , $ function );
291
+ Route::get ('/post/{post}/tag-id/{tag:id} ' , $ function );
292
+ Route::get ('/post/{post}/tag-slug/{tag:slug} ' , $ function );
293
+ });
294
+
295
+ $ response = $ this ->getJson ("/post/ {$ post ->id }/tag/ {$ tag ->slug }" );
296
+ $ response ->assertJsonFragment (['id ' => $ tag ->id ]);
297
+
298
+ $ response = $ this ->getJson ("/post/ {$ post ->id }/tag-id/ {$ tag ->id }" );
299
+ $ response ->assertJsonFragment (['id ' => $ tag ->id ]);
300
+
301
+ $ response = $ this ->getJson ("/post/ {$ post ->id }/tag-slug/ {$ tag ->slug }" );
302
+ $ response ->assertJsonFragment (['id ' => $ tag ->id ]);
303
+ }
264
304
}
265
305
266
306
class ImplicitBindingUser extends Model
@@ -287,6 +327,25 @@ class ImplicitBindingPost extends Model
287
327
public $ table = 'posts ' ;
288
328
289
329
protected $ fillable = ['user_id ' ];
330
+
331
+ public function tags ()
332
+ {
333
+ return $ this ->hasMany (ImplicitBindingTag::class, 'post_id ' );
334
+ }
335
+ }
336
+
337
+ class ImplicitBindingTag extends Model
338
+ {
339
+ use HasUlids;
340
+
341
+ public $ table = 'tags ' ;
342
+
343
+ protected $ fillable = ['slug ' , 'post_id ' ];
344
+
345
+ public function getRouteKeyName ()
346
+ {
347
+ return 'slug ' ;
348
+ }
290
349
}
291
350
292
351
class ImplicitBindingComment extends Model
0 commit comments