Files
alrahma_sunday_school_api/app/Models/Stats.php
T
root e13df69885
API CI/CD / Validate (composer + pint) (push) Successful in 3m6s
API CI/CD / Test (PHPUnit) (push) Failing after 4m53s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 59s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
fix unittests issues
2026-07-07 20:56:32 -04:00

64 lines
1.4 KiB
PHP

<?php
namespace App\Models;
class Stats extends BaseModel
{
protected $table = 'stats';
/**
* If your stats table has created_at/updated_at, keep true (default).
* If it doesn't, set to false.
*/
public $timestamps = true;
protected $fillable = ['students', 'teachers', 'admins', 'users'];
protected $casts = [
'students' => 'integer',
'teachers' => 'integer',
'admins' => 'integer',
'users' => 'integer',
];
public function getFillable(): array
{
return $this->fillable;
}
/**
* legacy-compatible: getStats()
* Returns all rows.
*/
public static function getStats()
{
return static::query()->get();
}
/**
* Enhancement: if you treat stats as a single row (common),
* this returns the first row, creating it with zeros if missing.
*/
public static function singleton(): self
{
return static::query()->first() ?? static::query()->create([
'students' => 0,
'teachers' => 0,
'admins' => 0,
'users' => 0,
]);
}
/**
* Enhancement: update singleton stats safely.
*/
public static function updateSingleton(array $data): self
{
$row = static::singleton();
$row->fill($data);
$row->save();
return $row;
}
}