*/ protected $fillable = [ 'tenant_id', 'sender_id', 'recipient_id', 'subject', 'message', 'is_read', 'read_at', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'is_read' => 'boolean', 'read_at' => 'datetime', ]; /** * Get the club that owns the message. */ public function tenant(): BelongsTo { return $this->belongsTo(Tenant::class); } /** * Get the sender of the message. */ public function sender(): BelongsTo { return $this->belongsTo(User::class, 'sender_id'); } /** * Get the recipient of the message. */ public function recipient(): BelongsTo { return $this->belongsTo(User::class, 'recipient_id'); } /** * Mark message as read. */ public function markAsRead(): void { $this->update([ 'is_read' => true, 'read_at' => now(), ]); } /** * Scope a query to only include unread messages. */ public function scopeUnread($query) { return $query->where('is_read', false); } /** * Scope a query to only include read messages. */ public function scopeRead($query) { return $query->where('is_read', true); } }