WatchApi.php 984 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Http\Api;
  4. use App\Models\Like;
  5. use App\Models\Notification;
  6. use Illuminate\Support\Str;
  7. class WatchApi
  8. {
  9. public static function change(array $resId, string $from, string $message)
  10. {
  11. // 发送站内信
  12. $watches = Like::where('type', 'watch')
  13. ->whereIn('target_id', $resId)
  14. ->get();
  15. $notifications = [];
  16. foreach ($watches as $watch) {
  17. $notifications[] = [
  18. 'id' => Str::uuid(),
  19. 'from' => $from,
  20. 'to' => $watch->user_id,
  21. 'url' => '',
  22. 'content' => $message,
  23. 'res_type' => $watch->target_type,
  24. 'res_id' => $watch->target_id,
  25. 'channel' => Str::uuid(),
  26. 'created_at' => now(),
  27. 'updated_at' => now(),
  28. ];
  29. }
  30. $new = Notification::insert($notifications);
  31. return $new;
  32. }
  33. }