migrants-nt-sec/app/Http/Controllers/DashboardController.php

54 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Person;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
class DashboardController extends Controller
{
public function getStats()
{
// Total migrants count
$totalMigrants = Person::count();
// New migrants in the current month
$currentMonthStart = Carbon::now()->startOfMonth();
$newThisMonth = Person::where('created_at', '>=', $currentMonthStart)->count();
// Recent additions (last 30 days)
$thirtyDaysAgo = Carbon::now()->subDays(30);
$recentAdditions = Person::where('created_at', '>=', $thirtyDaysAgo)->count();
// Pending reviews - example: people with missing information
$pendingReviews = Person::whereNull('date_of_birth')
->orWhereNull('place_of_birth')
->orWhereNull('occupation')
->count();
// Incomplete records - persons missing multiple key fields
$incompleteRecords = Person::where(function($query) {
$query->whereNull('date_of_birth')
->orWhereNull('place_of_birth');
})
->where(function($query) {
$query->whereNull('occupation')
->orWhereNull('reference')
->orWhereNull('id_card_no');
})
->count();
return response()->json([
'success' => true,
'data' => [
'total_migrants' => $totalMigrants,
'new_this_month' => $newThisMonth,
'recent_additions' => $recentAdditions,
'pending_reviews' => $pendingReviews,
'incomplete_records' => $incompleteRecords
]
]);
}
}