Files
newznab-tmux/app/Models/GrabStat.php
T
2024-09-10 22:22:01 +02:00

28 lines
779 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class GrabStat extends Model
{
use HasFactory;
protected $guarded = [];
public static function insertTopGrabbers(): void
{
$users = User::query()->selectRaw('id, username, SUM(grabs) as grabs')->groupBy('id', 'username')->having('grabs', '>', 0)->orderByDesc('grabs')->limit(10)->get();
// Insert data into the grab_stats table
foreach ($users as $user) {
self::updateOrCreate(['username' => $user->username], ['grabs' => $user->grabs]);
}
}
public static function getTopGrabbers(): array
{
return self::query()->select(['username', 'grabs'])->get()->toArray();
}
}