| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Notification;
- use Carbon\Carbon;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Support\Facades\Auth;
- class NotificationController extends Controller
- {
- public function index(): JsonResponse
- {
- $user = Auth::user();
- $notifications = Notification::where('user_id', $user->id)
- ->orderBy('read', 'asc')
- ->orderBy('created_at', 'desc')
- ->limit(50)
- ->get()
- ->map(function ($notification) {
- return [
- 'id' => $notification->id,
- 'title' => $notification->title,
- 'description' => $notification->description,
- 'origin' => $notification->origin,
- 'origin_id' => $notification->origin_id,
- 'type' => $notification->type,
- 'read' => $notification->read,
- 'time' => Carbon::parse(
- $notification->created_at
- )->diffForHumans(),
- ];
- });
- return $this->successResponse(
- payload: $notifications
- );
- }
- public function markAsRead(int $id): JsonResponse
- {
- $notification = Notification::where('id', $id)
- ->where('user_id', Auth::id())
- ->firstOrFail();
- $notification->update([
- 'read' => true,
- 'read_at' => now(),
- ]);
- return $this->successResponse(
- message: __('messages.updated')
- );
- }
- public function markAllAsRead(): JsonResponse
- {
- Notification::where('user_id', Auth::id())
- ->where('read', false)
- ->update([
- 'read' => true,
- 'read_at' => now(),
- ]);
- return $this->successResponse(
- message: __('messages.updated')
- );
- }
- }
|