Update Lihat Jumlah Kunjungan
This commit is contained in:
@@ -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