You've already forked Epicnabbo-Catalogus-Updated-Daily
90 lines
2.1 KiB
PHP
90 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Miscellaneous;
|
|
|
|
use App\Models\Room;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class CameraWeb extends Model
|
|
{
|
|
protected $table = 'camera_web';
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
public $timestamps = false;
|
|
|
|
/**
|
|
* @param Builder<CameraWeb> $query
|
|
*/
|
|
#[\Illuminate\Database\Eloquent\Attributes\Scope]
|
|
protected function period(Builder $query, string $period): void
|
|
{
|
|
if ($period == 'today') {
|
|
$query->where('timestamp', '>=', \Illuminate\Support\Facades\Date::today()->timestamp);
|
|
}
|
|
|
|
if ($period == 'last_week') {
|
|
$query->whereBetween('timestamp', [now()->subWeek()->timestamp, now()->timestamp]);
|
|
}
|
|
|
|
if ($period == 'last_month') {
|
|
$query->whereBetween('timestamp', [now()->subMonth()->timestamp, now()->timestamp]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Room, $this>
|
|
*/
|
|
public function room(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Room::class);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<CameraLike, $this>
|
|
*/
|
|
public function likes(): HasMany
|
|
{
|
|
return $this->hasMany(CameraLike::class);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<CameraView, $this>
|
|
*/
|
|
public function views(): HasMany
|
|
{
|
|
return $this->hasMany(CameraView::class);
|
|
}
|
|
|
|
/**
|
|
* @return Attribute<string, never>
|
|
*/
|
|
protected function formattedDate(): Attribute
|
|
{
|
|
/** @var Attribute<string, never> */
|
|
return new Attribute(
|
|
get: fn () => \Illuminate\Support\Facades\Date::parse($this->timestamp)->format('Y-m-d H:i'),
|
|
);
|
|
}
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'timestamp' => 'datetime',
|
|
];
|
|
}
|
|
}
|