Skip to content

Commit a21ca92

Browse files
committed
Added boot method to set name property for mailable
Signed-off-by: snipe <[email protected]>
1 parent 260174d commit a21ca92

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

app/Models/User.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
140140
'manager' => ['first_name', 'last_name', 'username'],
141141
];
142142

143+
144+
/**
145+
* This sets the name property on the user. It's not a real field in the database
146+
* (since we use first_name and last_name, but the Laravel mailable method
147+
* uses this to determine the name of the user to send emails to.
148+
*
149+
* We only have to do this on the User model and no other models because other
150+
* first-class objects have a name field.
151+
* @return void
152+
*/
153+
154+
protected static function boot()
155+
{
156+
parent::boot();
157+
158+
static::retrieved(function($user){
159+
$user->name = $user->getFullNameAttribute();
160+
});
161+
}
162+
163+
143164
/**
144165
* Internally check the user permission for the given section
145166
*
@@ -280,6 +301,7 @@ public function isActivated()
280301
return $this->activated == 1;
281302
}
282303

304+
283305
/**
284306
* Returns the full name attribute
285307
*
@@ -847,29 +869,18 @@ public function scopeOrderCompany($query, $order)
847869

848870

849871

850-
/**
851-
* This is a mutator to allow the emails to fall back to the system settings if no locale is set.
852-
*
853-
* @return Attribute
854-
*
855-
*/
856-
protected function locale(): Attribute
857-
{
858-
return Attribute::make(
859-
set: fn ($value) => $value ?? Setting::getSettings()->locale,
860-
);
861-
}
862872

863873
/**
864874
* Get the preferred locale for the user.
865-
* This is used via the HasLocalePreference interface, which is part of Laravel. This would
866-
* normally work natively, but because we allow the override in the Settings model and we also
867-
* manually set it in the notifications (for now), this doesn't handle all use cases.
875+
*
876+
* This uses the HasLocalePreference contract to determine the user's preferred locale,
877+
* used by Laravel's mail system to determine the locale for sending emails.
878+
* https://laravel.com/docs/11.x/mail#user-preferred-locales
868879
*
869880
*/
870-
public function preferredLocale()
881+
public function preferredLocale(): string
871882
{
872-
return $this->locale;
883+
return $this->locale ?? Setting::getSettings()->locale ?? config('app.locale');
873884
}
874885

875886
public function getUserTotalCost(){

0 commit comments

Comments
 (0)