File tree 4 files changed +29
-2
lines changed
4 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -64,7 +64,7 @@ public function roles(): BelongsToMany
64
64
public function users (): BelongsToMany
65
65
{
66
66
return $ this ->morphedByMany (
67
- getModelForGuard ($ this ->attributes ['guard_name ' ]),
67
+ getModelForGuard ($ this ->attributes ['guard_name ' ] ?? config ( ' auth.defaults.guard ' ) ),
68
68
'model ' ,
69
69
config ('permission.table_names.model_has_permissions ' ),
70
70
PermissionRegistrar::$ pivotPermission ,
Original file line number Diff line number Diff line change @@ -70,7 +70,7 @@ public function permissions(): BelongsToMany
70
70
public function users (): BelongsToMany
71
71
{
72
72
return $ this ->morphedByMany (
73
- getModelForGuard ($ this ->attributes ['guard_name ' ]),
73
+ getModelForGuard ($ this ->attributes ['guard_name ' ] ?? config ( ' auth.defaults.guard ' ) ),
74
74
'model ' ,
75
75
config ('permission.table_names.model_has_roles ' ),
76
76
PermissionRegistrar::$ pivotRole ,
Original file line number Diff line number Diff line change 7
7
8
8
class PermissionTest extends TestCase
9
9
{
10
+ /** @test */
11
+ public function it_get_user_models_using_with ()
12
+ {
13
+ $ this ->testUser ->givePermissionTo ($ this ->testUserPermission );
14
+
15
+ $ permission = app (Permission::class)::with ('users ' )
16
+ ->where ($ this ->testUserPermission ->getKeyName (), $ this ->testUserPermission ->getKey ())
17
+ ->first ();
18
+
19
+ $ this ->assertEquals ($ permission ->getKey (), $ this ->testUserPermission ->getKey ());
20
+ $ this ->assertCount (1 , $ permission ->users );
21
+ $ this ->assertEquals ($ permission ->users [0 ]->id , $ this ->testUser ->id );
22
+ }
23
+
10
24
/** @test */
11
25
public function it_throws_an_exception_when_the_permission_already_exists ()
12
26
{
Original file line number Diff line number Diff line change @@ -21,6 +21,19 @@ public function setUp(): void
21
21
Permission::create (['name ' => 'wrong-guard-permission ' , 'guard_name ' => 'admin ' ]);
22
22
}
23
23
24
+ /** @test */
25
+ public function it_get_user_models_using_with ()
26
+ {
27
+ $ this ->testUser ->assignRole ($ this ->testUserRole );
28
+
29
+ $ role = app (Role::class)::with ('users ' )
30
+ ->where ($ this ->testUserRole ->getKeyName (), $ this ->testUserRole ->getKey ())->first ();
31
+
32
+ $ this ->assertEquals ($ role ->getKey (), $ this ->testUserRole ->getKey ());
33
+ $ this ->assertCount (1 , $ role ->users );
34
+ $ this ->assertEquals ($ role ->users [0 ]->id , $ this ->testUser ->id );
35
+ }
36
+
24
37
/** @test */
25
38
public function it_has_user_models_of_the_right_class ()
26
39
{
You can’t perform that action at this time.
0 commit comments