You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: original-en/container.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -178,6 +178,22 @@ $this->app->singletonIf(Transistor::class, function (Application $app) {
178
178
});
179
179
```
180
180
181
+
Alternatively, you may mark an interface or class with the `#[Singleton]` attribute to indicate to the container that it should be resolved one time:
182
+
183
+
```php
184
+
<?php
185
+
186
+
namespace App\Services;
187
+
188
+
use Illuminate\Container\Attributes\Singleton;
189
+
190
+
#[Singleton]
191
+
class Transistor
192
+
{
193
+
// ...
194
+
}
195
+
```
196
+
181
197
<aname="binding-scoped"></a>
182
198
#### Binding Scoped Singletons
183
199
@@ -201,6 +217,22 @@ $this->app->scopedIf(Transistor::class, function (Application $app) {
201
217
});
202
218
```
203
219
220
+
Alternatively, you may mark an interface or class with the `#[Scoped]` attribute to indicate to the container that it should be resolved one time within a given Laravel request / job lifecycle:
Internally, Laravel already ignores some types of errors for you, such as exceptions resulting from 404 HTTP errors or 419 HTTP responses generated by invalid CSRF tokens. If you would like to instruct Laravel to stop ignoring a given type of exception, you may use the `stopIgnoring` exception method in your application's `bootstrap/app.php` file:
-[Dispatching Events After Database Transactions](#dispatching-events-after-database-transactions)
@@ -455,6 +457,62 @@ class SendShipmentNotification implements ShouldQueueAfterCommit
455
457
> [!NOTE]
456
458
> 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).
457
459
460
+
<aname="queued-listener-middleware"></a>
461
+
### Queued Listener Middleware
462
+
463
+
Queued listeners can also utilize [job middleware](/docs/{{version}}/queues#job-middleware). Job middleware allow you to wrap custom logic around the execution of queued listeners, reducing boilerplate in the listeners themselves. After creating job middleware, they may be attached to a listener by returning them from the listener's `middleware` method:
464
+
465
+
```php
466
+
<?php
467
+
468
+
namespace App\Listeners;
469
+
470
+
use App\Events\OrderShipped;
471
+
use App\Jobs\Middleware\RateLimited;
472
+
use Illuminate\Contracts\Queue\ShouldQueue;
473
+
474
+
class SendShipmentNotification implements ShouldQueue
475
+
{
476
+
/**
477
+
* Handle the event.
478
+
*/
479
+
public function handle(OrderShipped $event): void
480
+
{
481
+
// Process the event...
482
+
}
483
+
484
+
/**
485
+
* Get the middleware the listener should pass through.
486
+
*
487
+
* @return array<int,object>
488
+
*/
489
+
public function middleware(OrderShipped $event): array
490
+
{
491
+
return [new RateLimited];
492
+
}
493
+
}
494
+
```
495
+
496
+
<aname="encrypted-queued-listeners"></a>
497
+
#### Encrypted Queued Listeners
498
+
499
+
Laravel allows you to ensure the privacy and integrity of a queued listener's data via [encryption](/docs/{{version}}/encryption). To get started, simply add the `ShouldBeEncrypted` interface to the listener class. Once this interface has been added to the class, Laravel will automatically encrypt your listener before pushing it onto a queue:
500
+
501
+
```php
502
+
<?php
503
+
504
+
namespace App\Listeners;
505
+
506
+
use App\Events\OrderShipped;
507
+
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
508
+
use Illuminate\Contracts\Queue\ShouldQueue;
509
+
510
+
class SendShipmentNotification implements ShouldQueue, ShouldBeEncrypted
511
+
{
512
+
// ...
513
+
}
514
+
```
515
+
458
516
<aname="handling-failed-jobs"></a>
459
517
### Handling Failed Jobs
460
518
@@ -540,7 +598,7 @@ If both `retryUntil` and `tries` are defined, Laravel gives precedence to the `r
540
598
<aname="specifying-queued-listener-backoff"></a>
541
599
#### Specifying Queued Listener Backoff
542
600
543
-
If you would like to configure how many seconds Laravel should wait before retrying a listener that has encountered an exception, you may do so by defining a `backoff` property on your listener class:
601
+
If you would like to configure how many seconds Laravel should wait before retrying a listener that has encountered an exception, you may do so by defining a `$backoff` property on your listener class:
544
602
545
603
```php
546
604
/**
@@ -557,7 +615,7 @@ If you require more complex logic for determining the listeners's backoff time,
557
615
/**
558
616
* Calculate the number of seconds to wait before retrying the queued listener.
559
617
*/
560
-
public function backoff(): int
618
+
public function backoff(OrderShipped $event): int
561
619
{
562
620
return 3;
563
621
}
@@ -571,12 +629,101 @@ You may easily configure "exponential" backoffs by returning an array of backoff
571
629
*
572
630
* @return list<int>
573
631
*/
574
-
public function backoff(): array
632
+
public function backoff(OrderShipped $event): array
Sometimes you may wish to specify that a queued listener may be attempted many times, but should fail if the retries are triggered by a given number of unhandled exceptions (as opposed to being released by the `release` method directly). To accomplish this, you may define a `$maxExceptions` property on your listener class:
642
+
643
+
```php
644
+
<?php
645
+
646
+
namespace App\Listeners;
647
+
648
+
use App\Events\OrderShipped;
649
+
use Illuminate\Contracts\Queue\ShouldQueue;
650
+
use Illuminate\Queue\InteractsWithQueue;
651
+
652
+
class SendShipmentNotification implements ShouldQueue
653
+
{
654
+
use InteractsWithQueue;
655
+
656
+
/**
657
+
* The number of times the queued listener may be attempted.
658
+
*
659
+
* @var int
660
+
*/
661
+
public $tries = 25;
662
+
663
+
/**
664
+
* The maximum number of unhandled exceptions to allow before failing.
665
+
*
666
+
* @var int
667
+
*/
668
+
public $maxExceptions = 3;
669
+
670
+
/**
671
+
* Handle the event.
672
+
*/
673
+
public function handle(OrderShipped $event): void
674
+
{
675
+
// Process the event...
676
+
}
677
+
}
678
+
```
679
+
680
+
In this example, the listener will be retried up to 25 times. However, the listener will fail if three unhandled exceptions are thrown by the listener.
681
+
682
+
<aname="specifying-queued-listener-timeout"></a>
683
+
#### Specifying Queued Listener Timeout
684
+
685
+
Often, you know roughly how long you expect your queued listeners to take. For this reason, Laravel allows you to specify a "timeout" value. If a listener is processing for longer than the number of seconds specified by the timeout value, the worker processing the listener will exit with an error. You may define the maximum number of seconds a listener should be allowed to run by defining a `$timeout` property on your listener class:
686
+
687
+
```php
688
+
<?php
689
+
690
+
namespace App\Listeners;
691
+
692
+
use App\Events\OrderShipped;
693
+
use Illuminate\Contracts\Queue\ShouldQueue;
694
+
695
+
class SendShipmentNotification implements ShouldQueue
696
+
{
697
+
/**
698
+
* The number of seconds the listener can run before timing out.
699
+
*
700
+
* @var int
701
+
*/
702
+
public $timeout = 120;
703
+
}
704
+
```
705
+
706
+
If you would like to indicate that a listener should be marked as failed on timeout, you may define the `$failOnTimeout` property on the listener class:
707
+
708
+
```php
709
+
<?php
710
+
711
+
namespace App\Listeners;
712
+
713
+
use App\Events\OrderShipped;
714
+
use Illuminate\Contracts\Queue\ShouldQueue;
715
+
716
+
class SendShipmentNotification implements ShouldQueue
717
+
{
718
+
/**
719
+
* Indicate if the listener should be marked as failed on timeout.
@@ -695,7 +694,7 @@ public function toMail(object $notifiable): MailMessage
695
694
}
696
695
```
697
696
698
-
If your application is using the Mailgun driver, you may consult Mailgun's documentation for more information on [tags](https://documentation.mailgun.com/en/latest/user_manual.html#tagging-1) and [metadata](https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages). Likewise, the Postmark documentation may also be consulted for more information on their support for [tags](https://postmarkapp.com/blog/tags-support-for-smtp) and [metadata](https://postmarkapp.com/support/article/1125-custom-metadata-faq).
697
+
If your application is using the Mailgun driver, you may consult Mailgun's documentation for more information on [tags](https://documentation.mailgun.com/docs/mailgun/user-manual/tracking-messages/#tags) and [metadata](https://documentation.mailgun.com/docs/mailgun/user-manual/sending-messages/#attaching-metadata-to-messages). Likewise, the Postmark documentation may also be consulted for more information on their support for [tags](https://postmarkapp.com/blog/tags-support-for-smtp) and [metadata](https://postmarkapp.com/support/article/1125-custom-metadata-faq).
699
698
700
699
If your application is using Amazon SES to send emails, you should use the `metadata` method to attach [SES "tags"](https://docs.aws.amazon.com/ses/latest/APIReference/API_MessageTag.html) to the message.
701
700
@@ -992,6 +991,16 @@ foreach ($user->unreadNotifications as $notification) {
992
991
}
993
992
```
994
993
994
+
If you want to retrieve only the "read" notifications, you may use the `readNotifications` relationship:
995
+
996
+
```php
997
+
$user = App\Models\User::find(1);
998
+
999
+
foreach ($user->readNotifications as $notification) {
1000
+
echo $notification->type;
1001
+
}
1002
+
```
1003
+
995
1004
> [!NOTE]
996
1005
> 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.
997
1006
@@ -1361,7 +1370,6 @@ If a notification supports being sent as a Slack message, you should define a `t
1361
1370
```php
1362
1371
use Illuminate\Notifications\Slack\BlockKit\Blocks\ContextBlock;
1363
1372
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
1364
-
use Illuminate\Notifications\Slack\BlockKit\Composites\ConfirmObject;
Sometimes, applications store each user's preferred locale. By implementing the `HasLocalePreference` contract on your notifiable model, you may instruct Laravel to use this stored locale when sending a notification:
0 commit comments