Issues (416)

app/Models/ReleaseStat.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\DB;
8
9
class ReleaseStat extends Model
10
{
11
    use HasFactory;
0 ignored issues
show
The trait Illuminate\Database\Eloquent\Factories\HasFactory requires the property $factoryClass which is not provided by App\Models\ReleaseStat.
Loading history...
12
13
    protected $guarded = [];
14
15
    public static function insertRecentlyAdded(): void
16
    {
17
        $categories = Category::query()->with('parent')->where('r.adddate', '>', now()->subWeek())->select([
18
            'root_categories_id', DB::raw('COUNT(r.id) as count'), 'title',
19
        ])->join('releases as r', 'r.categories_id', '=',
20
            'categories.id')->groupBy('title')->orderByDesc('count')->get();
21
22
        foreach ($categories as $category) {
23
            // Check if we already have the information and if we do just update the count
24
            if (self::query()->where('category', $category->title)->exists()) {
25
                self::query()->where('category', $category->title)->update(['count' => $category->count]);
26
27
                continue;
28
            }
29
            self::query()->create(['category' => $category->title, 'count' => $category->count]);
30
        }
31
    }
32
33
    public static function getRecentlyAdded(): array
34
    {
35
        return self::query()->select(['category', 'count'])->get()->toArray();
36
    }
37
}
38