WatchApi.php 1.1 KB

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