2022-05-16 04:07:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-10-04 05:35:46 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2022-05-16 04:07:35 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-10-04 05:35:46 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2022-05-16 04:07:35 +02:00
|
|
|
|
|
|
|
class SeedBoxes extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
protected $table = 'seedboxes';
|
|
|
|
|
2022-07-22 15:49:18 +02:00
|
|
|
protected $keyType = 'string';
|
|
|
|
|
2022-05-16 04:07:35 +02:00
|
|
|
public $incrementing = false;
|
|
|
|
|
|
|
|
protected $fillable = ['id', 'active', 'title', 'hostname', 'seed_box_type', 'provider_id', 'location_id', 'bandwidth', 'port_speed', 'disk', 'disk_type', 'disk_as_gb', 'was_promo', 'owned_since'];
|
|
|
|
|
2022-10-04 05:35:46 +02:00
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::addGlobalScope('order', function (Builder $builder) {
|
2022-10-31 01:14:26 +01:00
|
|
|
$array = Settings::orderByProcess(Session::get('sort_on') ?? 2);//created_at desc if not set
|
2022-10-04 05:35:46 +02:00
|
|
|
if (!in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
|
|
|
|
$builder->orderBy($array[0], $array[1]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-20 06:35:37 +02:00
|
|
|
public static function allSeedboxes()
|
|
|
|
{//All seedboxes and relationships (no using joins)
|
|
|
|
return Cache::remember("all_seedboxes", now()->addMonth(1), function () {
|
2022-10-20 04:57:04 +02:00
|
|
|
$query = SeedBoxes::with(['location', 'provider', 'price']);
|
|
|
|
if (in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
|
|
|
|
$options = Settings::orderByProcess(Session::get('sort_on'));
|
|
|
|
$query->orderBy(Pricing::select("pricings.$options[0]")->whereColumn("pricings.service_id", "seedboxes.id"), $options[1]);
|
|
|
|
}
|
|
|
|
return $query->get();
|
2022-07-20 06:35:37 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function seedbox(string $seedbox_id)
|
|
|
|
{//Single seedbox and relationships (no using joins)
|
|
|
|
return Cache::remember("seedbox.$seedbox_id", now()->addMonth(1), function () use ($seedbox_id) {
|
|
|
|
return SeedBoxes::where('id', $seedbox_id)
|
2022-10-13 04:43:09 +02:00
|
|
|
->with(['location', 'provider', 'price'])->first();
|
2022-07-20 06:35:37 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function location()
|
2022-05-16 04:07:35 +02:00
|
|
|
{
|
2022-07-20 06:35:37 +02:00
|
|
|
return $this->hasOne(Locations::class, 'id', 'location_id');
|
2022-05-16 04:07:35 +02:00
|
|
|
}
|
|
|
|
|
2022-07-20 06:35:37 +02:00
|
|
|
public function provider()
|
2022-05-16 04:07:35 +02:00
|
|
|
{
|
2022-07-20 06:35:37 +02:00
|
|
|
return $this->hasOne(Providers::class, 'id', 'provider_id');
|
2022-05-16 04:07:35 +02:00
|
|
|
}
|
|
|
|
|
2022-07-20 06:35:37 +02:00
|
|
|
public function price()
|
2022-05-16 04:07:35 +02:00
|
|
|
{
|
2022-07-20 06:35:37 +02:00
|
|
|
return $this->hasOne(Pricing::class, 'service_id', 'id');
|
2022-05-16 04:07:35 +02:00
|
|
|
}
|
|
|
|
|
2022-07-20 06:35:37 +02:00
|
|
|
public function labels()
|
|
|
|
{
|
|
|
|
return $this->hasMany(LabelsAssigned::class, 'service_id', 'id');
|
|
|
|
}
|
2022-05-16 04:07:35 +02:00
|
|
|
|
|
|
|
}
|