kithkin/app/Models/Event.php

74 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
class Event extends Model
{
/** table and key */
protected $table = 'calendarobjects'; // Sabre table
protected $primaryKey = 'id';
public $timestamps = false; // no created_at / updated_at
/** mass assignment */
protected $fillable = [
'calendarid',
'uid',
'uri',
'lastmodified',
'etag',
'size',
'componenttype',
'calendardata',
'firstoccurence',
'lastoccurence',
];
/** column casting */
protected $casts = [
'lastmodified' => 'integer',
'firstoccurence' => 'integer',
'lastoccurence' => 'integer',
'size' => 'integer',
];
/**
* relationships
**/
public function calendar(): BelongsTo
{
return $this->belongsTo(Calendar::class, 'calendarid');
}
public function meta(): HasOne
{
return $this->hasOne(EventMeta::class, 'event_id');
}
/**
* scopes and helpers
**/
public function scopeInRange($query, $start, $end)
{
return $query->whereHas('meta', function ($q) use ($start, $end) {
$q->where('start_at', '<=', $end)
->where(function ($qq) use ($start) {
$qq->where('end_at', '>=', $start)
->orWhereNull('end_at');
});
});
}
public static function forCalendarsInRange($calendarIds, $start, $end)
{
return static::query()
->with('meta')
->whereIn('calendarid', $calendarIds)
->inRange($start, $end)
->get();
}
}