forge/app/Livewire/ModIndex.php

63 lines
1.8 KiB
PHP
Raw Normal View History

2024-08-09 10:15:05 -04:00
<?php
namespace App\Livewire;
use App\Models\Mod;
use App\Models\SptVersion;
2024-08-09 11:19:07 -04:00
use Illuminate\Support\Str;
2024-08-09 10:15:05 -04:00
use Livewire\Component;
use Livewire\WithPagination;
class ModIndex extends Component
{
use WithPagination;
public string $modSearch = '';
2024-08-09 11:19:07 -04:00
public string $sectionFilter = 'featured';
public string $versionFilter = '';
2024-08-09 10:15:05 -04:00
public function render()
{
2024-08-09 11:45:37 -04:00
// 'featured' section is default
$section = 'featured';
2024-08-09 11:19:07 -04:00
switch ($this->sectionFilter) {
2024-08-09 11:45:37 -04:00
case 'new':
$section = 'created_at';
break;
2024-08-09 11:19:07 -04:00
case 'most_downloaded':
$section = 'total_downloads';
break;
case 'recently_updated':
$section = 'updated_at';
break;
case 'top_rated':
// probably use some kind of 'likes' or something
// not implemented yet afaik -waffle
break;
}
2024-08-09 10:15:05 -04:00
$mods = Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at'])
->withTotalDownloads()
->with(['latestVersion', 'latestVersion.sptVersion', 'users:id,name'])
->whereHas('latestVersion')
->whereHas('latestVersion.sptVersion', function ($query) {
$query->where('version', 'like', '%'.Str::trim($this->versionFilter).'%');
})
->where('name', 'like', '%'.Str::trim($this->modSearch).'%')
2024-08-09 11:19:07 -04:00
->orderByDesc($section)
2024-08-09 10:15:05 -04:00
->paginate(12);
$sptVersions = SptVersion::select(['id', 'version', 'color_class'])->orderByDesc('version')->get();
return view('livewire.mod-index', ['mods' => $mods, 'sptVersions' => $sptVersions]);
2024-08-09 10:15:05 -04:00
}
public function changeSection($section): void
{
$this->sectionFilter = $section;
}
}