Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ The `notify` method that is provided by this trait expects to receive a notifica

$user->notify(new InvoicePaid($invoice));

> [!NOTE]
> [!NOTE]
> Remember, you may use the `Notifiable` trait on any of your models. You are not limited to only including it on your `User` model.

<a name="using-the-notification-facade"></a>
Expand All @@ -116,7 +116,7 @@ You can also send notifications immediately using the `sendNow` method. This met

Every notification class has a `via` method that determines on which channels the notification will be delivered. Notifications may be sent on the `mail`, `database`, `broadcast`, `vonage`, and `slack` channels.

> [!NOTE]
> [!NOTE]
> If you would like to use other delivery channels such as Telegram or Pusher, check out the community driven [Laravel Notification Channels website](http://laravel-notification-channels.com).

The `via` method receives a `$notifiable` instance, which will be an instance of the class to which the notification is being sent. You may use `$notifiable` to determine which channels the notification should be delivered on:
Expand All @@ -134,7 +134,7 @@ The `via` method receives a `$notifiable` instance, which will be an instance of
<a name="queueing-notifications"></a>
### Queueing Notifications

> [!WARNING]
> [!WARNING]
> Before queueing notifications you should configure your queue and [start a worker](/docs/{{version}}/queues#running-the-queue-worker).

Sending notifications can take time, especially if the channel needs to make an external API call to deliver the notification. To speed up your application's response time, let your notification be queued by adding the `ShouldQueue` interface and `Queueable` trait to your class. The interface and trait are already imported for all notifications generated using the `make:notification` command, so you may immediately add them to your notification class:
Expand Down Expand Up @@ -305,7 +305,7 @@ Alternatively, you may call the `afterCommit` method from your notification's co
}
}

> [!NOTE]
> [!NOTE]
> To learn more about working around these issues, please review the documentation regarding [queued jobs and database transactions](/docs/{{version}}/queues#jobs-and-database-transactions).

<a name="determining-if-the-queued-notification-should-be-sent"></a>
Expand Down Expand Up @@ -375,14 +375,14 @@ The `MailMessage` class contains a few simple methods to help you build transact
->line('Thank you for using our application!');
}

> [!NOTE]
> [!NOTE]
> Note we are using `$this->invoice->id` in our `toMail` method. You may pass any data your notification needs to generate its message into the notification's constructor.

In this example, we register a greeting, a line of text, a call to action, and then another line of text. These methods provided by the `MailMessage` object make it simple and fast to format small transactional emails. The mail channel will then translate the message components into a beautiful, responsive HTML email template with a plain-text counterpart. Here is an example of an email generated by the `mail` channel:

<img src="https://laravel.com/img/docs/notification-example-2.png">

> [!NOTE]
> [!NOTE]
> When sending mail notifications, be sure to set the `name` configuration option in your `config/app.php` configuration file. This value will be used in the header and footer of your mail notification messages.

<a name="error-messages"></a>
Expand Down Expand Up @@ -542,7 +542,7 @@ To add attachments to an email notification, use the `attach` method while build
->attach('/path/to/file');
}

> [!NOTE]
> [!NOTE]
> The `attach` method offered by notification mail messages also accepts [attachable objects](/docs/{{version}}/mail#attachable-objects). Please consult the comprehensive [attachable object documentation](/docs/{{version}}/mail#attachable-objects) to learn more.

When attaching files to a message, you may also specify the display name and / or MIME type by passing an `array` as the second argument to the `attach` method:
Expand Down Expand Up @@ -834,7 +834,7 @@ php artisan make:notifications-table
php artisan migrate
```

> [!NOTE]
> [!NOTE]
> If your notifiable models are using [UUID or ULID primary keys](/docs/{{version}}/eloquent#uuid-and-ulid-keys), you should replace the `morphs` method with [`uuidMorphs`](/docs/{{version}}/migrations#column-method-uuidMorphs) or [`ulidMorphs`](/docs/{{version}}/migrations#column-method-ulidMorphs) in the notification table migration.

<a name="formatting-database-notifications"></a>
Expand All @@ -855,18 +855,26 @@ If a notification supports being stored in a database table, you should define a
];
}

When the notification is stored in your application's database, the `type` column will be populated with the notification's class name. However, you may customize this behavior by defining a `databaseType` method on your notification class:
When a notification is stored in your application's database, the `type` column will be set to the notification's class name by default, and the `read_at` column will be `null`. However, you can customize this behavior by defining the `databaseType` and `initialDatabaseReadAtValue` methods in your notification class:

use Illuminate\Support\Carbon;

/**
* Get the notification's database type.
*
* @return string
*/
public function databaseType(object $notifiable): string
{
return 'invoice-paid';
}

/**
* Get the initial value for the "read_at" column.
*/
public function initialDatabaseReadAtValue(): ?Carbon
{
return null;
}

<a name="todatabase-vs-toarray"></a>
#### `toDatabase` vs. `toArray`

Expand All @@ -891,7 +899,7 @@ If you want to retrieve only the "unread" notifications, you may use the `unread
echo $notification->type;
}

> [!NOTE]
> [!NOTE]
> To access your notifications from your JavaScript client, you should define a notification controller for your application which returns the notifications for a notifiable entity, such as the current user. You may then make an HTTP request to that controller's URL from your JavaScript client.

<a name="marking-notifications-as-read"></a>
Expand Down Expand Up @@ -1196,7 +1204,7 @@ Instead of using the fluent message builder methods to construct your Block Kit
"type": "plain_text",
"text": "Team Announcement"
}
},
},
{
"type": "section",
"text": {
Expand Down Expand Up @@ -1241,7 +1249,7 @@ In the following example, which utilizes the `actionsBlock` method, Slack will s
->actionsBlock(function (ActionsBlock $block) {
// ID defaults to "button_acknowledge_invoice"...
$block->button('Acknowledge Invoice')->primary();

// Manually configure the ID...
$block->button('Deny')->danger()->id('deny_invoice');
});
Expand Down Expand Up @@ -1330,7 +1338,7 @@ For instance, returning `#support-channel` from the `routeNotificationForSlack`
<a name="notifying-external-slack-workspaces"></a>
### Notifying External Slack Workspaces

> [!NOTE]
> [!NOTE]
> Before sending notifications to external Slack workspaces, your Slack App must be [distributed](#slack-app-distribution).

Of course, you will often want to send notifications to the Slack workspaces owned by your application's users. To do so, you will first need to obtain a Slack OAuth token for the user. Thankfully, [Laravel Socialite](/docs/{{version}}/socialite) includes a Slack driver that will allow you to easily authenticate your application's users with Slack and [obtain a bot token](/docs/{{version}}/socialite#slack-bot-scopes).
Expand Down