creation of the create and get routes for Notifications

This commit is contained in:
2025-11-14 17:41:01 +01:00
parent 3c2128b9b7
commit 901c6ab68e
3 changed files with 48 additions and 0 deletions
@@ -0,0 +1,38 @@
<?php
namespace App\Http\Controllers;
use App\Models\Notification;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class NotificationsController extends Controller
{
public function create(Request $request, int $id): JsonResponse {
$notification = Notification::create([
"content" => $request["content"]
]);
$notification->users()->attach($id);
return response()->json(['message' => 'Notification created successfully']);
}
public function getNotifications(Request $request, int $id): JsonResponse {
try {
$user = User::findOrFail($id);
return response()->json([
'data' => $user->notifications
]);
} catch(\Exception $e) {
Log::info($e->getMessage());
return response()->json(['message' => "Server error"], 500);
}
}
}