takeone/app/Models/ClubMessage.php

96 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ClubMessage extends Model
{
use HasFactory;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'club_messages';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'tenant_id',
'sender_id',
'recipient_id',
'subject',
'message',
'is_read',
'read_at',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
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);
}
}