You've already forked Atomcms-edit
94 lines
2.7 KiB
PHP
Executable File
94 lines
2.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models\Miscellaneous;
|
|
|
|
use App\Models\Concerns\BelongsToUser;
|
|
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\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property int|null $room_id
|
|
* @property string $url
|
|
* @property string $caption
|
|
* @property int $timestamp
|
|
* @property int $likes
|
|
* @property int $views
|
|
* @property-read Carbon $timestamp
|
|
* @property-read string $formattedDate
|
|
* @property-read User|null $user
|
|
* @property-read Room|null $room
|
|
* @property-read Collection<int, CameraLike> $likesRelation
|
|
* @property-read Collection<int, CameraView> $views
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|CameraWeb where($column, $operator = null, $value = null)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|CameraWeb latest($column = 'created_at')
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|CameraWeb count($columns = '*')
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|CameraWeb take(int $value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|CameraWeb with($relations)
|
|
*/
|
|
class CameraWeb extends Model
|
|
{
|
|
use BelongsToUser;
|
|
|
|
#[\Override]
|
|
protected $table = 'camera_web';
|
|
|
|
#[\Override]
|
|
protected $guarded = ['id', 'created_at', 'updated_at', 'user_id', 'image', 'caption'];
|
|
|
|
#[\Override]
|
|
public $timestamps = false;
|
|
|
|
/** @var array<string, string> */
|
|
#[\Override]
|
|
protected $casts = [
|
|
'timestamp' => 'datetime',
|
|
];
|
|
|
|
public function scopePeriod(Builder $query, $period): void
|
|
{
|
|
if ($period == 'today') {
|
|
$query->where('timestamp', '>=', Carbon::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]);
|
|
}
|
|
}
|
|
|
|
public function room(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Room::class);
|
|
}
|
|
|
|
public function likes(): HasMany
|
|
{
|
|
return $this->hasMany(CameraLike::class);
|
|
}
|
|
|
|
public function views(): HasMany
|
|
{
|
|
return $this->hasMany(CameraView::class);
|
|
}
|
|
|
|
public function formattedDate(): Attribute
|
|
{
|
|
return new Attribute(
|
|
get: fn () => Carbon::parse($this->timestamp)->format('Y-m-d H:i'),
|
|
);
|
|
}
|
|
}
|