Update Lihat Jumlah Kunjungan
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Web\Konfigurasi;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Konfigurasi\FilterLogAktivitasRequest;
|
||||
use App\Models\Konfigurasi\KunjunganPengguna;
|
||||
use App\Models\Konfigurasi\LogAktivitas;
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\User;
|
||||
@@ -64,6 +65,7 @@ class LogAktivitasController extends Controller
|
||||
'jenis' => self::JENIS,
|
||||
'perPage' => self::PER_PAGE,
|
||||
'namaPetani' => $namaPetani,
|
||||
'kunjungan' => KunjunganPengguna::ringkasan(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\Konfigurasi\KunjunganPengguna;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class CatatKunjungan
|
||||
{
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$response = $next($request);
|
||||
|
||||
$user = $request->user() ?? $request->user('sanctum');
|
||||
if (! $user) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$tanda = today()->toDateString() . ':' . $user->id;
|
||||
|
||||
// Web: tanda di session, tanpa query tambahan
|
||||
if ($request->hasSession()) {
|
||||
if ($request->session()->get('kunjungan_tercatat') !== $tanda) {
|
||||
KunjunganPengguna::catat($user->id);
|
||||
$request->session()->put('kunjungan_tercatat', $tanda);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
// API: tanda di cache sampai akhir hari
|
||||
if (Cache::add("kunjungan:{$tanda}", true, now()->endOfDay())) {
|
||||
KunjunganPengguna::catat($user->id);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Konfigurasi;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class KunjunganPengguna extends Model
|
||||
{
|
||||
public const UPDATED_AT = null;
|
||||
|
||||
protected $table = 'kunjungan_pengguna';
|
||||
|
||||
protected $fillable = ['tanggal', 'user_id'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['tanggal' => 'date'];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// Catat kunjungan (abaikan jika hari ini sudah tercatat)
|
||||
public static function catat(int $userId): void
|
||||
{
|
||||
static::query()->insertOrIgnore([
|
||||
'tanggal' => today()->toDateString(),
|
||||
'user_id' => $userId,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
// Ringkasan pengunjung hari ini, bulan ini, dan total (satu query)
|
||||
public static function ringkasan(): object
|
||||
{
|
||||
return static::query()
|
||||
->selectRaw('count(*) filter (where tanggal = ?) as hari_ini', [today()->toDateString()])
|
||||
->selectRaw('count(distinct user_id) filter (where tanggal >= ?) as bulan_ini', [today()->startOfMonth()->toDateString()])
|
||||
->selectRaw('count(*) filter (where tanggal >= ?) as kunjungan_bulan_ini', [today()->startOfMonth()->toDateString()])
|
||||
->selectRaw('count(*) as total_kunjungan')
|
||||
->selectRaw('count(distinct user_id) as total_pengguna')
|
||||
->toBase()
|
||||
->first();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user