---
title: "Preparing your models — Messages for Laravel | Roundly"
description: "Implement ParticipatesInMessaging and add HasMessaging to any model that sends messages or takes part in threads."
url: https://roundly-consulting.com/open-source/docs/messages-for-laravel/preparing-models
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Messages for Laravel](https://roundly-consulting.com/open-source/docs/messages-for-laravel.md)

# Preparing your models

Any model that takes part in messaging — a sender or a participant — must implement RoundlyConsulting\\Messages\\Interfaces\\ParticipatesInMessaging. Add the HasMessaging trait to get the ergonomic API:

```php
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Messages\Concerns\HasMessaging;
use RoundlyConsulting\Messages\Interfaces\ParticipatesInMessaging;

class User extends Model implements ParticipatesInMessaging
{
    use HasMessaging;

    public function participateAs(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }
}
```

## What participateAs() is for

participateAs() returns the public shape of a participant. Keep it to fields that are safe to show other people in the conversation:

- Broadcast payloads — the sender of a message, the participant on join/read/leave, and the typist on a typing signal.
- JSON resources — MessageResource.sender and ParticipantResource.participant, through the default participant resource. A model without the interface renders there as its key and class basename ({"id": 12, "type": "Restaurant"}) — see API resources.
- System messages — joins and leaves store it under meta.participant, and its name key is what they show.

Broadcasting a model that does not implement the interface throws ParticipationException::interfaceImplementationRequired().

## Any model can participate

Participation is polymorphic, so different model types — a User and a Company, a customer and a support team — can share the same conversation:

```php
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Messages\Concerns\HasMessaging;
use RoundlyConsulting\Messages\Interfaces\ParticipatesInMessaging;

class Company extends Model implements ParticipatesInMessaging
{
    use HasMessaging;

    public function participateAs(): array
    {
        return ['id' => $this->id, 'name' => $this->name];
    }
}

// A User and a Company share one conversation
$thread = $customer->startConversationWith($company, name: 'Order A-1042');
$company->sendMessageTo($thread, 'Your order ships tomorrow.');
```

They share the sender\_id and participant\_id columns, so every participating model must use the same primary-key type — set key\_type to match it (see Key types).

## The HasMessaging API

Everything the trait adds, at a glance. It is shorthand over the same MessagesManager the Messages facade uses, so every write runs the same action and Messages::fake() records it like a facade call:

```php
$user->participations();                          // MorphMany<Participant>
$user->threads();                                 // Collection<Thread>, newest activity first
$user->conversations();                           // alias of threads()
$user->startConversationWith([$bob], name: 'Q3'); // Thread — $user becomes the owner
$user->conversationWith($bob);                    // Thread — find-or-create the DM
$user->sendMessageTo($thread, 'Hello');           // Message
$user->joinThread($thread);                       // Participant — self-join, only on threads open to everyone
$user->unreadThreads();                           // Collection<Thread> with unread messages
$user->unreadCount();                             // int — across every thread
$user->unreadCount($thread);                      // int — within one thread
$user->markThreadRead($thread);                   // Participant
```

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/messages-for-laravel.md)
