はじめに
イベントが起きたらメールで通知、出したいですよね。
ドキュメント 読んでもやり方が分からなくて詰まったので備忘録的に残したいと思います。
準備[共通]
0-1.Notification作成
console
php artisan make:notification SendInvitation
0-2.Notificationクラスを編集する
App\Notifications\SendInvitation.php
<?php
namespace App\Notifications\Invitation;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
+ class SendInvitation extends Notification implements ShouldQueue // Queueスタックして後からメール送信するようにした
{
use Queueable;
+ protected $user_name;
+ public function __construct($user_name)
+ {
+ $this->user_name= $user_name;
+ }
...
+ public function toMail($notifiable)
+ {
+ $mail = new MailMessage();
+ $mail->subject('招待されています')
+ ->greeting('こんにちは!')
+ ->line("{$this->user_name} さんから招待が届いています。")
+ ;
+ return $mail;
+ }
...
コピペ用
App\Notifications\SendInvitation.php
<?php
namespace App\Notifications\Invitation;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class SendInvitation extends Notification implements ShouldQueue // Queueスタックして後からメール送信するようにした
{
use Queueable;
protected $user_name;
public function __construct($user_name)
{
$this->user_name= $user_name;
}
...
public function toMail($notifiable)
{
$mail = new MailMessage();
$mail->subject('サービスに招待されています')
->greeting('こんにちは!')
->line("{$this->user_name} さんから招待が届いています。")
;
return $mail;
}
...
1.Modelから通知を送信する方法
Eloquent Model
内にメールアドレスカラムがある場合はこちらが便利みたいです。
他のメールアドレスに送信したい場合は 2.ServiceクラスやControllerクラスから通知を送信させる方法 をご覧ください。
関係ないけど「特定の人にメールを送信する」やり方が分からなくて詰みかけた。
1-1.ModelにNotifiableトレイトを装備させる
\App\User.php
<?php
namespace App;
+ use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
+ use Notifiable;
...
1-2.Model内で通知イベントを作成する
\App\User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
...
+ public function sendInvitation ($user_name) {
// ここで先ほど作成したNotificationクラスを呼び出す
+ $this->notify(new \App\Notifications\SendInvitation($user_name));
+ }
1-3.通知を送信する
\App\Http\Controller\HogeController
public function invite ($id) {
$user = \App\User::find($id);
// 適当な処理
// 処理後に通知を送信する
$user->sendInvitation(\Auth::user()->name);
}
2.ServiceクラスやControllerクラスから通知を送信する方法
2-1.通知を送信する
\App\Http\Controller\HogeController
// 忘れないこと
use App\Notifications\SendInvitation;
public function invite ($id) {
$user = \App\User::find($id);
// 適当な処理
// 処理後に通知を送信する
\Notification::send($user, new \App\Notifications\SendInvitation(\Auth::user()->name));
}
どう違うのか?
1.はModelに紐付いているので、意識することなくメールを送信できそう。
2.は不特定多数にメールしたいとき、メール先を変えたいときなどに役立ちそう。
->to('MailAdress')
なんかで送信先を変えられないかと試行錯誤してみましたが、なんか違うっぽい。
1.にしろ2.にしろ、メールアドレスはデフォルトだと email
カラムを参照しているみたいなので、独自のカラム(例: email_address
)を設定したい場合、下記のようにModelに追記する必要があるみたい。
\App\User.php
...
class User extends Authenticatable
{
...
+ public function routeNotificationForMail()
+ {
+ return $this->email_address;
+ }
...
}
今後の展望
Slack通知機能実装したい。