2024-05-15 00:31:24 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-05-31 17:44:52 -04:00
|
|
|
use App\Models\Scopes\DisabledScope;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2024-05-28 17:19:36 -04:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
2024-05-31 17:44:52 -04:00
|
|
|
/**
|
|
|
|
* @property string $slug
|
|
|
|
*/
|
2024-05-15 00:31:24 -04:00
|
|
|
class Mod extends Model
|
|
|
|
{
|
|
|
|
use HasFactory, SoftDeletes;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'user_id',
|
|
|
|
'name',
|
|
|
|
'slug',
|
|
|
|
'description',
|
|
|
|
'license_id',
|
|
|
|
'source_code_link',
|
|
|
|
];
|
|
|
|
|
2024-05-31 17:44:52 -04:00
|
|
|
protected static function booted(): void
|
|
|
|
{
|
|
|
|
static::addGlobalScope(new DisabledScope);
|
|
|
|
}
|
|
|
|
|
2024-05-24 17:06:02 -04:00
|
|
|
protected function slug(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: fn (string $value) => strtolower($value),
|
|
|
|
set: fn (string $value) => Str::slug($value),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-15 00:31:24 -04:00
|
|
|
public function user(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function license(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(License::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function versions(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(ModVersion::class);
|
|
|
|
}
|
|
|
|
|
2024-05-24 17:06:02 -04:00
|
|
|
public function scopeWithTotalDownloads($query)
|
2024-05-17 17:11:54 -04:00
|
|
|
{
|
2024-05-31 17:44:52 -04:00
|
|
|
return $query->addSelect(['total_downloads' => ModVersion::selectRaw('SUM(downloads) AS total_downloads')
|
2024-05-24 17:06:02 -04:00
|
|
|
->whereColumn('mod_id', 'mods.id'),
|
|
|
|
]);
|
2024-05-17 23:54:03 -04:00
|
|
|
}
|
|
|
|
|
2024-05-24 17:06:02 -04:00
|
|
|
public function latestSptVersion(): BelongsTo
|
2024-05-17 23:54:03 -04:00
|
|
|
{
|
2024-05-24 17:06:02 -04:00
|
|
|
return $this->belongsTo(ModVersion::class, 'latest_spt_version_id');
|
2024-05-17 17:11:54 -04:00
|
|
|
}
|
|
|
|
|
2024-05-24 17:06:02 -04:00
|
|
|
public function scopeWithLatestSptVersion($query)
|
2024-05-15 00:31:24 -04:00
|
|
|
{
|
2024-05-24 17:06:02 -04:00
|
|
|
return $query
|
|
|
|
->addSelect(['latest_spt_version_id' => ModVersion::select('id')
|
|
|
|
->whereColumn('mod_id', 'mods.id')
|
|
|
|
->orderByDesc(
|
|
|
|
SptVersion::select('version')
|
|
|
|
->whereColumn('mod_versions.spt_version_id', 'spt_versions.id')
|
2024-06-01 23:04:06 -04:00
|
|
|
->orderByDesc('version')
|
2024-05-24 17:06:02 -04:00
|
|
|
->take(1),
|
|
|
|
)
|
2024-06-01 23:04:06 -04:00
|
|
|
->orderByDesc('version')
|
2024-05-24 17:06:02 -04:00
|
|
|
->take(1),
|
|
|
|
])
|
2024-05-31 17:44:52 -04:00
|
|
|
->havingNotNull('latest_spt_version_id')
|
2024-05-24 17:06:02 -04:00
|
|
|
->with(['latestSptVersion', 'latestSptVersion.sptVersion']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function lastUpdatedVersion(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(ModVersion::class, 'last_updated_spt_version_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeWithLastUpdatedVersion($query)
|
|
|
|
{
|
|
|
|
return $query
|
|
|
|
->addSelect(['last_updated_spt_version_id' => ModVersion::select('id')
|
|
|
|
->whereColumn('mod_id', 'mods.id')
|
|
|
|
->orderByDesc('updated_at')
|
|
|
|
->take(1),
|
|
|
|
])
|
|
|
|
->orderByDesc(
|
|
|
|
ModVersion::select('updated_at')
|
|
|
|
->whereColumn('mod_id', 'mods.id')
|
|
|
|
->orderByDesc('updated_at')
|
|
|
|
->take(1)
|
|
|
|
)
|
2024-05-31 17:44:52 -04:00
|
|
|
->havingNotNull('last_updated_spt_version_id')
|
2024-05-24 17:06:02 -04:00
|
|
|
->with(['lastUpdatedVersion', 'lastUpdatedVersion.sptVersion']);
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|
|
|
|
}
|