2022-03-05 16:02:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-10-04 05:35:46 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2022-03-05 16:02:12 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-07-20 06:35:12 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-10-04 05:35:46 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2022-03-05 16:02:12 +01:00
|
|
|
|
|
|
|
class Misc extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
public $incrementing = false;
|
|
|
|
|
|
|
|
protected $table = 'misc_services';
|
|
|
|
|
2022-07-22 15:49:18 +02:00
|
|
|
protected $keyType = 'string';
|
|
|
|
|
2022-03-05 16:02:12 +01:00
|
|
|
protected $fillable = ['id', 'name', 'owned_since'];
|
2022-07-20 06:35:12 +02:00
|
|
|
|
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:12 +02:00
|
|
|
public static function allMisc()
|
|
|
|
{//All misc and relationships (no using joins)
|
|
|
|
return Cache::remember("all_misc", now()->addMonth(1), function () {
|
2022-10-20 04:57:04 +02:00
|
|
|
$query = Misc::with(['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", "misc_services.id"), $options[1]);
|
|
|
|
}
|
|
|
|
return $query->get();
|
2022-07-20 06:35:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function misc(string $misc_id)
|
|
|
|
{//Single misc and relationships (no using joins)
|
|
|
|
return Cache::remember("misc.$misc_id", now()->addMonth(1), function () use ($misc_id) {
|
|
|
|
return Misc::where('id', $misc_id)
|
2022-09-22 05:20:23 +02:00
|
|
|
->with(['price'])->first();
|
2022-07-20 06:35:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function price()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Pricing::class, 'service_id', 'id');
|
|
|
|
}
|
|
|
|
|
2022-03-05 16:02:12 +01:00
|
|
|
}
|