Skip to main content
The Message model represents individual messages within conversations. It supports text messages, attachments, replies, and disappearing messages.

Namespace

Wirechat\Wirechat\Models\Message

Properties

id
int
Unique identifier for the message
conversation_id
int|null
ID of the conversation this message belongs to
participant_id
int
ID of the participant who sent the message
reply_id
int|null
ID of the message being replied to (if this is a reply)
body
string|null
The message text content
type
MessageType
Type of message (TEXT, ATTACHMENT, etc.)
kept_at
Carbon|null
Timestamp when message was kept from disappearing
deleted_at
Carbon|null
Soft delete timestamp
created_at
Carbon|null
Message creation timestamp
updated_at
Carbon|null
Last update timestamp

Relationships

conversation()

Get the conversation this message belongs to.
public function conversation(): BelongsTo
return
BelongsTo
Relationship to Conversation model
Example:
$conversation = $message->conversation;
echo $conversation->type;

participant()

Get the participant who sent the message.
public function participant(): BelongsTo
return
BelongsTo
Relationship to Participant model
Example:
$participant = $message->participant;
$user = $participant->participantable;
echo $user->name;

attachment()

Get the attachment associated with this message.
public function attachment(): MorphOne
return
MorphOne
Polymorphic relationship to Attachment model

parent()

Get the message being replied to.
public function parent(): BelongsTo
return
BelongsTo
Relationship to parent Message (includes trashed)
Example:
if ($message->hasParent()) {
    $parentMessage = $message->parent;
    echo "Replying to: " . $parentMessage->body;
}

reply()

Get replies to this message.
public function reply(): HasOne
return
HasOne
Relationship to reply Message

Accessors

user

Get the actual user model who sent the message.
public function getUserAttribute(): Model
return
Model
The underlying user model (participantable)
Example:
$user = $message->user;
echo $user->name; // Direct access to user

sendable (Deprecated)

Legacy accessor for getting the sender. Use user instead.
public function getSendableAttribute(): Model
return
Model
The user model (same as user attribute)

Message Status

readBy()

Check if the message has been read by a user.
public function readBy(Model|Participant $user): bool
user
Model|Participant
required
The user or participant to check
return
bool
True if the message has been read
Example:
if ($message->readBy(auth()->user())) {
    echo "Message has been read";
}

ownedBy()

Check if the message is owned by a specific user.
public function ownedBy($user): bool
user
Model
required
The user to check ownership against
return
bool
True if the message belongs to the user

belongsToAuth()

Check if the message belongs to the authenticated user.
public function belongsToAuth(): bool
return
bool
True if the message belongs to the authenticated user

Attachment Methods

hasAttachment()

Check if the message has an attachment.
public function hasAttachment(): bool
return
bool
True if an attachment exists

isAttachment()

Check if the message type is ATTACHMENT.
public function isAttachment(): bool
return
bool
True if message type is ATTACHMENT
Example:
if ($message->isAttachment()) {
    $attachment = $message->attachment;
    echo $attachment->url;
}

Reply Methods

hasReply()

Check if this message has replies.
public function hasReply(): bool
return
bool
True if replies exist

hasParent()

Check if this message is a reply to another message.
public function hasParent(): bool
return
bool
True if this is a reply
Example:
if ($message->hasParent()) {
    $originalMessage = $message->parent;
    echo "In reply to: {$originalMessage->body}";
}

if ($message->hasReply()) {
    echo "This message has replies";
}

Message Management

deleteFor()

Delete the message for a specific user only.
public function deleteFor(Model|Authenticatable $user): bool|null
user
Model|Authenticatable
required
The user deleting the message
return
bool|null
True if force deleted, null otherwise
Example:
// Delete for current user only
$message->deleteFor(auth()->user());

deleteForEveryone()

Delete the message for all participants.
public function deleteForEveryone(Model $user): void
user
Model
required
The user requesting deletion (must own the message or be an admin)
Example:
// Delete for everyone (requires ownership or admin)
$message->deleteForEveryone(auth()->user());

Content Methods

isEmoji()

Check if the message body contains only emojis.
public function isEmoji(): bool
return
bool
True if the message contains only emoji characters
Example:
if ($message->isEmoji()) {
    // Render with larger emoji size
    echo '<span class="large-emoji">' . $message->body . '</span>';
}

Query Scopes

whereIsNotOwnedBy()

Scope to exclude messages owned by a specific user.
public function scopeWhereIsNotOwnedBy($query, Model|Authenticatable $user): Builder
query
Builder
required
The query builder instance
user
Model|Authenticatable
required
The user to exclude
Example:
// Get messages not sent by current user
$messages = Message::whereIsNotOwnedBy(auth()->user())->get();

Example Usage

use Wirechat\Wirechat\Models\Message;

// Create a message
$message = Message::create([
    'conversation_id' => $conversation->id,
    'participant_id' => $participant->id,
    'body' => 'Hello, world!',
    'type' => MessageType::TEXT,
]);

// Check ownership
if ($message->ownedBy(auth()->user())) {
    echo "You sent this message";
}

// Get sender
$sender = $message->user;
echo "Sent by: {$sender->name}";

Notes

  • Messages are soft-deleted by default using Laravel’s SoftDeletes trait
  • The WithoutRemovedMessages global scope automatically filters deleted messages
  • Deleting a message also deletes its attachment and associated actions
  • Messages with replies are only soft-deleted, not force-deleted
  • In private conversations, messages are force-deleted when both participants delete them