34 lines
848 B
PHP
34 lines
848 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class Announcement extends Model
|
|
{
|
|
protected $fillable = [
|
|
'image_id', 'title', 'description', 'link', 'start_date', 'end_date',
|
|
];
|
|
|
|
protected $casts = [
|
|
'start_date' => 'datetime',
|
|
'end_date' => 'datetime',
|
|
];
|
|
|
|
public function image(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Image::class);
|
|
}
|
|
|
|
// Currently within its [start_date, end_date] window (null bounds = open-ended).
|
|
public function scopeActive($query)
|
|
{
|
|
$now = now();
|
|
|
|
return $query
|
|
->where(fn ($q) => $q->whereNull('start_date')->orWhere('start_date', '<=', $now))
|
|
->where(fn ($q) => $q->whereNull('end_date')->orWhere('end_date', '>=', $now));
|
|
}
|
|
}
|