【发布时间】:2022-01-02 05:38:16
【问题描述】:
我正在寻找基于用户区域设置值(英语和西班牙语)在 Laravel 中构建通知的最佳方法。
SomeController.php(发送通知):
Notification::send(User::find($some_id), new CustomNotification($some_parameter));
CustomNotification.php:
class CustomNotification extends Notification
{
use Queueable;
protected $title, $body, $foot;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($some_parameter)
{
$this->title = __('Some Title');
$this->response = __('Some Response');
$this->foot = 'My WebPage Title';
}
...
}
使用 CustomNotification 构造,$title 和 $response 的值将根据发送通知的当前用户进行转换,但在这种情况下,发送通知的是管理员,因此此变量的值将在管理员区域设置,而不是用户。
【问题讨论】:
-
您可能必须在
Notification::send()调用之外定义$user = User::find($some_id),以便您可以将其传递给new CustomNotification($some_parameter, $user)调用,然后您可以根据传递的@987654327 执行app()->setLocale()@ 在调用那些__()方法之前。
标签: php laravel eloquent push-notification