69 lines
1.2 KiB
PHP
69 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class UserNotification extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'waktu_makan_id',
|
|
'title',
|
|
'message',
|
|
'type',
|
|
'is_read',
|
|
'read_at',
|
|
'data'
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_read' => 'boolean',
|
|
'read_at' => 'datetime',
|
|
'data' => 'array'
|
|
];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function waktuMakan()
|
|
{
|
|
return $this->belongsTo(WaktuMakan::class);
|
|
}
|
|
|
|
public function scopeUnread($query)
|
|
{
|
|
return $query->where('is_read', false);
|
|
}
|
|
|
|
public function scopeRead($query)
|
|
{
|
|
return $query->where('is_read', true);
|
|
}
|
|
|
|
public function scopeByType($query, $type)
|
|
{
|
|
return $query->where('type', $type);
|
|
}
|
|
|
|
public function markAsRead()
|
|
{
|
|
$this->update([
|
|
'is_read' => true,
|
|
'read_at' => now()
|
|
]);
|
|
}
|
|
|
|
public function markAsUnread()
|
|
{
|
|
$this->update([
|
|
'is_read' => false,
|
|
'read_at' => null
|
|
]);
|
|
}
|
|
}
|