Метод line
Метод line применяется к объекту MailMessage
и добавляет текстовую строку в тело письма.
Этот метод используется внутри метода toMail
класса уведомления для построения содержимого email.
Метод возвращает тот же объект MailMessage,
что позволяет использовать цепочки вызовов.
В качестве параметра передаётся строка текста или массив строк.
Синтаксис
<?php
use Illuminate\Notifications\Messages\MailMessage;
$message = (new MailMessage())
->line($text);
?>
Пример
Давайте создадим простое уведомление с одной текстовой строкой:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class SimpleNotification extends Notification
{
use Queueable;
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->line('Welcome to our application!');
}
}
?>
Результат выполнения кода:
"Welcome to our application!"
Пример
Давайте добавим несколько строк текста в письмо, передав массив:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class WelcomeNotification 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([
'Thank you for registering on our website.',
'Please verify your email address to get started.',
])
->action('Verify Email', url('/verify'))
->line('If you did not create an account, no further action is required.');
}
}
?>
Результат выполнения кода:
"Hello John
Thank you for registering on our website.
Please verify your email address to get started.
Verify Email
If you did not create an account, no further action is required."
Пример
Давайте используем метод line в уведомлении с несколькими блоками текста:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
use App\Models\Order;
class OrderNotification extends Notification
{
use Queueable;
protected $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Order Confirmation #' . $this->order->id)
->greeting('Hello!')
->line('Your order has been confirmed.')
->line('Order details:')
->line('Order #: ' . $this->order->id)
->line('Total: $' . $this->order->total)
->line('Status: ' . $this->order->status)
->action('View Order', url('/orders/' . $this->order->id))
->line('Thank you for your purchase!');
}
}
?>
Результат выполнения кода:
"Hello!
Your order has been confirmed.
Order details:
Order #: 12345
Total: $150.50
Status: processing
View Order
Thank you for your purchase!"
Пример
Давайте передадим в метод line строку с HTML-разметкой:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class InvoiceNotification extends Notification
{
use Queueable;
protected $invoiceNumber;
protected $amount;
public function __construct($invoiceNumber, $amount)
{
$this->invoiceNumber = $invoiceNumber;
$this->amount = $amount;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Invoice #' . $this->invoiceNumber)
->greeting('Dear Customer,')
->line('Your invoice <strong>#' . $this->invoiceNumber . '</strong> has been generated.')
->line('Amount due: <strong>$' . $this->amount . '</strong>')
->line('Please pay before the due date.')
->action('Pay Invoice', url('/pay/' . $this->invoiceNumber))
->line('If you have any questions, contact our support team.')
->salutation('Best regards, Finance Team');
}
}
?>
Результат выполнения кода:
"Dear Customer,
Your invoice #INV-2026-001 has been generated.
Amount due: $250.00
Please pay before the due date.
Pay Invoice
If you have any questions, contact our support team.
Best regards, Finance Team"
Смотрите также
-
метод
greeting,
который добавляет приветствие в письмо -
метод
action,
который добавляет кнопку с ссылкой в письмо -
метод
salutation,
который добавляет прощальную фразу в письмо -
класс
MailMessage,
который используется для построения почтовых уведомлений