Skip to content

Commit 8125446

Browse files
authored
Merge pull request #2219 from erikn69/erikn69-patch-fix
Fix undefined index guard_name
2 parents 1cb0a5c + e4493da commit 8125446

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/Models/Permission.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function roles(): BelongsToMany
6464
public function users(): BelongsToMany
6565
{
6666
return $this->morphedByMany(
67-
getModelForGuard($this->attributes['guard_name']),
67+
getModelForGuard($this->attributes['guard_name'] ?? config('auth.defaults.guard')),
6868
'model',
6969
config('permission.table_names.model_has_permissions'),
7070
PermissionRegistrar::$pivotPermission,

src/Models/Role.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function permissions(): BelongsToMany
7070
public function users(): BelongsToMany
7171
{
7272
return $this->morphedByMany(
73-
getModelForGuard($this->attributes['guard_name']),
73+
getModelForGuard($this->attributes['guard_name'] ?? config('auth.defaults.guard')),
7474
'model',
7575
config('permission.table_names.model_has_roles'),
7676
PermissionRegistrar::$pivotRole,

tests/PermissionTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88
class PermissionTest extends TestCase
99
{
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+
1024
/** @test */
1125
public function it_throws_an_exception_when_the_permission_already_exists()
1226
{

tests/RoleTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ public function setUp(): void
2121
Permission::create(['name' => 'wrong-guard-permission', 'guard_name' => 'admin']);
2222
}
2323

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+
2437
/** @test */
2538
public function it_has_user_models_of_the_right_class()
2639
{

0 commit comments

Comments
 (0)