Метод greeting
Метод greeting класса MailMessage применяется
для добавления приветствия в начало email-сообщения.
Метод принимает один параметр - строку с текстом приветствия.
По умолчанию, если метод не вызван, используется стандартное
приветствие "Hello!". Метод возвращает текущий экземпляр
MailMessage, что позволяет использовать цепочку вызовов.
Синтаксис
<?php
use Illuminate\Notifications\Messages\MailMessage;
$message = (new MailMessage)
->greeting($greetingText);
?>
Пример
Давайте создадим простое уведомление с приветствием "Welcome to our platform":
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class WelcomeNotification extends Notification
{
use Queueable;
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Welcome to our platform')
->line('Thank you for registering on our website.')
->action('Visit Site', url('/'))
->line('We hope you enjoy your experience!');
}
}
?>
Пример
Пример использования метода greeting в контроллере
для отправки уведомления пользователю:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Notifications\WelcomeNotification;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function register(Request $request)
{
// Сохранение пользователя...
$user = User::find(1);
$user->notify(new WelcomeNotification());
return response()->json(['message' => 'User registered']);
}
}
?>
Пример
Использование метода greeting с персонализированным
приветствием:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class CustomNotification extends Notification
{
use Queueable;
protected $userName;
public function __construct($userName)
{
$this->userName = $userName;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello, ' . $this->userName . '!')
->line('Your account has been successfully updated.')
->action('View Profile', route('profile'))
->salutation('Best regards, Administration');
}
}
?>
Смотрите также
-
метод
line,
который добавляет строку текста в письмо -
метод
action,
который добавляет кнопку с ссылкой в письмо -
метод
subject,
который устанавливает тему письма -
метод
salutation,
который добавляет завершающую фразу в письмо