store/app/Models/Notification.php

40 lines
853 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Guarded(['id'])]
class Notification extends Model
{
protected function casts(): array
{
return [
'is_read' => 'boolean',
'read_at' => 'datetime',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function markAsRead(): void
{
if (! $this->is_read) {
$this->update(['is_read' => true, 'read_at' => now()]);
}
}
#[Scope]
public function unread(Builder $query): void
{
$query->where('is_read', false);
}
}