reconstruction of the project
This commit is contained in:
+45
-3
@@ -7,7 +7,13 @@ use App\Models\BaseModel;
|
||||
class Stats extends BaseModel
|
||||
{
|
||||
protected $table = 'stats';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* If your stats table has created_at/updated_at, keep true (default).
|
||||
* If it doesn't, set to false.
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'students',
|
||||
'teachers',
|
||||
@@ -15,8 +21,44 @@ class Stats extends BaseModel
|
||||
'users',
|
||||
];
|
||||
|
||||
public function getStats()
|
||||
protected $casts = [
|
||||
'students' => 'integer',
|
||||
'teachers' => 'integer',
|
||||
'admins' => 'integer',
|
||||
'users' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* CI-compatible: getStats()
|
||||
* Returns all rows.
|
||||
*/
|
||||
public static function getStats()
|
||||
{
|
||||
return $this->findAll();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user