أُنشئ من Tokal/Test
Initial backend upload
هذا الالتزام موجود في:
47
app/Http/Controllers/Api/NotificationController.php
Normal file
47
app/Http/Controllers/Api/NotificationController.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Notification;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class NotificationController extends Controller
|
||||
{
|
||||
// LIST NOTIFICATIONS (auth)
|
||||
public function index(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'per_page' => 'nullable|integer|min:1|max:50',
|
||||
]);
|
||||
|
||||
$perPage = $validated['per_page'] ?? 10;
|
||||
|
||||
$paginator = Notification::query()
|
||||
->where('user_id', $request->user()->id)
|
||||
->orderByDesc('id')
|
||||
->paginate($perPage);
|
||||
|
||||
$data = collect($paginator->items())->map(function ($notification) {
|
||||
return [
|
||||
'id' => $notification->id,
|
||||
'type' => $notification->type,
|
||||
'title' => $notification->title,
|
||||
'body' => $notification->body,
|
||||
'data' => $notification->data_json,
|
||||
'is_read' => $notification->is_read,
|
||||
'created_at' => $notification->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'data' => $data,
|
||||
'meta' => [
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
المرجع في مشكلة جديدة
حظر مستخدم