Skip to content

Commit 0cd412d

Browse files
authored
Merge pull request #2837 from Ken-vdE/patch-2
Fix guard_name not used to set default attribute in Role and Permission model
2 parents 51a919e + 6f2f96f commit 0cd412d

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/Models/Permission.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Permission extends Model implements PermissionContract
2626

2727
public function __construct(array $attributes = [])
2828
{
29-
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
29+
$attributes['guard_name'] ??= Guard::getDefaultName(static::class);
3030

3131
parent::__construct($attributes);
3232

@@ -41,7 +41,7 @@ public function __construct(array $attributes = [])
4141
*/
4242
public static function create(array $attributes = [])
4343
{
44-
$attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
44+
$attributes['guard_name'] ??= Guard::getDefaultName(static::class);
4545

4646
$permission = static::getPermission(['name' => $attributes['name'], 'guard_name' => $attributes['guard_name']]);
4747

@@ -88,7 +88,7 @@ public function users(): BelongsToMany
8888
*/
8989
public static function findByName(string $name, ?string $guardName = null): PermissionContract
9090
{
91-
$guardName = $guardName ?? Guard::getDefaultName(static::class);
91+
$guardName ??= Guard::getDefaultName(static::class);
9292
$permission = static::getPermission(['name' => $name, 'guard_name' => $guardName]);
9393
if (! $permission) {
9494
throw PermissionDoesNotExist::create($name, $guardName);
@@ -106,7 +106,7 @@ public static function findByName(string $name, ?string $guardName = null): Perm
106106
*/
107107
public static function findById(int|string $id, ?string $guardName = null): PermissionContract
108108
{
109-
$guardName = $guardName ?? Guard::getDefaultName(static::class);
109+
$guardName ??= Guard::getDefaultName(static::class);
110110
$permission = static::getPermission([(new static)->getKeyName() => $id, 'guard_name' => $guardName]);
111111

112112
if (! $permission) {
@@ -123,7 +123,7 @@ public static function findById(int|string $id, ?string $guardName = null): Perm
123123
*/
124124
public static function findOrCreate(string $name, ?string $guardName = null): PermissionContract
125125
{
126-
$guardName = $guardName ?? Guard::getDefaultName(static::class);
126+
$guardName ??= Guard::getDefaultName(static::class);
127127
$permission = static::getPermission(['name' => $name, 'guard_name' => $guardName]);
128128

129129
if (! $permission) {

src/Models/Role.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Role extends Model implements RoleContract
2727

2828
public function __construct(array $attributes = [])
2929
{
30-
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
30+
$attributes['guard_name'] ??= Guard::getDefaultName(static::class);
3131

3232
parent::__construct($attributes);
3333

@@ -42,7 +42,7 @@ public function __construct(array $attributes = [])
4242
*/
4343
public static function create(array $attributes = [])
4444
{
45-
$attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
45+
$attributes['guard_name'] ??= Guard::getDefaultName(static::class);
4646

4747
$params = ['name' => $attributes['name'], 'guard_name' => $attributes['guard_name']];
4848
if (app(PermissionRegistrar::class)->teams) {
@@ -97,7 +97,7 @@ public function users(): BelongsToMany
9797
*/
9898
public static function findByName(string $name, ?string $guardName = null): RoleContract
9999
{
100-
$guardName = $guardName ?? Guard::getDefaultName(static::class);
100+
$guardName ??= Guard::getDefaultName(static::class);
101101

102102
$role = static::findByParam(['name' => $name, 'guard_name' => $guardName]);
103103

@@ -115,7 +115,7 @@ public static function findByName(string $name, ?string $guardName = null): Role
115115
*/
116116
public static function findById(int|string $id, ?string $guardName = null): RoleContract
117117
{
118-
$guardName = $guardName ?? Guard::getDefaultName(static::class);
118+
$guardName ??= Guard::getDefaultName(static::class);
119119

120120
$role = static::findByParam([(new static)->getKeyName() => $id, 'guard_name' => $guardName]);
121121

@@ -133,7 +133,7 @@ public static function findById(int|string $id, ?string $guardName = null): Role
133133
*/
134134
public static function findOrCreate(string $name, ?string $guardName = null): RoleContract
135135
{
136-
$guardName = $guardName ?? Guard::getDefaultName(static::class);
136+
$guardName ??= Guard::getDefaultName(static::class);
137137

138138
$role = static::findByParam(['name' => $name, 'guard_name' => $guardName]);
139139

0 commit comments

Comments
 (0)