أُنشئ من Tokal/Test
48 أسطر
1.4 KiB
PHP
48 أسطر
1.4 KiB
PHP
<?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(),
|
|
],
|
|
]);
|
|
}
|
|
}
|