やりたい事
- Laravel Auth でのユーザー登録 (register) 時に Registered イベントを発火
- Registered イベントに登録した UserRegisteredListener にて Notifiable トレイト notify() を実行。引数に指定した UserRegisterdNotifier を呼び出す
- UserRegisterdNotifier から mail, slack チャンネル通知をキューに追加し非同期で実行
課題・状況
- 同期処理(キューに追加しない)の場合うまく行くが、非同期で実行すると失敗する
- ワーカー上では「Processed」と表示され、正常に実行された事になっている
- notify() ではなく Notification ファザードの route() 関数で通知先を指定し非同期で実行すると、意図通り通知される
- Notifiable オブジェクト (User) からユーザー名や ID を取得してメッセージを組み立てたいため、Notification ファザードの route() 関数を利用した通知ではなく、 Notifiable トレイトの notify() もしくは Notification ファザードの send() を用いた通知としたい
- ちなみに Notification ファザードの send() を用いた通知でも Notifiable トレイト notify() の時と同様の挙動となる
- laravel.log でエラーが出力されず、状況が曖昧
環境 (Homestead)
- Laravel 7.30.4
- PHP 7.4.5
- MariaDB 14.14
- jobs テーブル作成済み、ワーカー(artisan queue:work)実行中
コード
app/Models/User.php
PHP
1<?php 2 3namespace App\Models; 4 5use Illuminate\Foundation\Auth\User as Authenticatable; 6use Illuminate\Notifications\Notifiable; 7use Illuminate\Notifications\Notification; 8use Auth; 9 10class User extends Authenticatable 11{ 12 use Notifiable; 13 14 public function routeNotificationForSlack(Notification $notification) 15 { 16 return config('app.slack_webhook_url'); 17 } 18} 19
app/Providers/EventServiceProvider.php
PHP
1<?php 2 3namespace App\Providers; 4 5use Illuminate\Auth\Events\Registered; 6use Illuminate\Auth\Listeners\SendEmailVerificationNotification; 7use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; 8use Illuminate\Support\Facades\Event; 9 10class EventServiceProvider extends ServiceProvider 11{ 12 protected $listen = [ 13 Registered::class => [ 14 SendEmailVerificationNotification::class, 15 'App\Listeners\UserRegisteredListener', 16 ] 17 ]; 18 19 public function boot() 20 { 21 parent::boot(); 22 } 23}
app/Listeners/UserRegisterdListener.php
PHP
1<?php 2 3namespace App\Listeners; 4 5use Illuminate\Auth\Events\Registered; 6use Illuminate\Contracts\Queue\ShouldQueue; 7use Illuminate\Queue\InteractsWithQueue; 8use App\Notifications\UserRegisterdNotifier; 9use Illuminate\Support\Facades\Notification; 10 11class UserRegisteredListener 12{ 13 public function handle(Registered $event) 14 { 15 //$event->user->notify(new UserRegisterdNotifier); // 失敗 16 // 以下成功 17 Notification::route('mail', $event->user->email) 18 ->route('slack', config('app.slack_webhook_url')) 19 ->notify(new UserRegisterdNotifier); 20 } 21} 22
app/Notifications/UserRegisterdNotifier.php
PHP
1<?php 2 3namespace App\Notifications; 4 5use Illuminate\Bus\Queueable; 6use Illuminate\Contracts\Queue\ShouldQueue; 7use Illuminate\Notifications\Messages\MailMessage; 8use Illuminate\Notifications\Messages\SlackMessage; 9use Illuminate\Notifications\Notification; 10use App\Models\User; 11 12class UserRegisterdNotifier extends Notification implements ShouldQueue 13{ 14 use Queueable; 15 16 public function via($notifiable) 17 { 18 return ['mail', 'slack']; 19 } 20 21 public function toMail($notifiable) 22 { 23 return (new MailMessage) 24 ->subject('[' . config('app.name') . '] ユーザー登録ありがとうございます') 25 ->greeting('ユーザー登録ありがとうございます') 26 ->line('引き続きよろしくお願い致します。'); 27 } 28 29 public function toSlack($notifiable) 30 { 31 if (!property_exists($notifiable, 'id')) { 32 $notifiable = User::orderBy('id', 'desc')->first(); // Notification ファザード用仮コード 33 } 34 return (new SlackMessage) 35 ->success() 36 ->content('ユーザー登録されました') 37 ->attachment(function ($attachment) use ($notifiable) { 38 $attachment->title($notifiable->username, url('/admin/users/' . $notifiable->id)) 39 ->content('Laravel Admin'); 40 }); 41 } 42}
コードは内容を一部省略しています。
状況が分かりづらく恐縮ですが、アドバイスいただけると助かります。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。