ArticleReplied.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Notifications;
  3. use App\Models\Reply;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Notifications\Notification;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Notifications\Messages\MailMessage;
  8. class ArticleReplied extends Notification implements ShouldQueue
  9. {
  10. use Queueable;
  11. public $reply;
  12. /**
  13. * Create a new notification instance.
  14. *
  15. * @return void
  16. */
  17. public function __construct(Reply $reply)
  18. {
  19. // 注入回复实体,方便 toDatabase 方法中的使用
  20. $this->reply = $reply;
  21. }
  22. /**
  23. * Get the notification's delivery channels.
  24. *
  25. * @param mixed $notifiable
  26. * @return array
  27. */
  28. public function via($notifiable)
  29. {
  30. // 开启通知的频道
  31. // return ['database', 'mail'];
  32. return ['database'];
  33. }
  34. public function toDatabase($notifiable)
  35. {
  36. $article = $this->reply->article;
  37. $link = $article->link();
  38. // 存入数据库里的数据
  39. return [
  40. 'reply_id' => $this->reply->id,
  41. 'reply_content' => $this->reply->content,
  42. 'user_id' => $this->reply->user->id,
  43. 'user_name' => $this->reply->user->name,
  44. 'article_link' => $link,
  45. 'article_id' => $article->id,
  46. 'article_title' => $article->title,
  47. ];
  48. }
  49. /**
  50. * Get the mail representation of the notification.
  51. *
  52. * @param mixed $notifiable
  53. * @return \Illuminate\Notifications\Messages\MailMessage
  54. */
  55. public function toMail($notifiable)
  56. {
  57. $url = $this->reply->article->link(['#reply' . $this->reply->id]);
  58. return (new MailMessage)
  59. ->line('你的文章有新回复!')
  60. ->action('查看回复', $url);
  61. }
  62. /**
  63. * Get the array representation of the notification.
  64. *
  65. * @param mixed $notifiable
  66. * @return array
  67. */
  68. public function toArray($notifiable)
  69. {
  70. return [
  71. //
  72. ];
  73. }
  74. }