user10139010
user10139010

Reputation:

Message relationship feature in Laravel

I am building a message system where a Team can send a message to all users and a user can send a message to one specific Team

I am having trouble trying to decide the relationships between the Team, Message, User Models

My idea would be to use the same table for sending messages with the sender_id and recipient_id changing depending on who created the message and who the recipient is so the id can either match a user profile or a team id. The type being defined as either broadcast if its a message from the team to all users and a contact if the user is sending a message to the team that way when i come to list messages i can filter by type etc.

Message table

Below is the table columns i had in mind for the Messages table:

 Schema::create('messages', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('sender_id');
        $table->integer('recipient_id');
        $table->string('type');
        $table->timestamps();
    });

Because a team can have many messages and many messages and can belong to many teams I was going to create a new pivot table called team_messages e.g

public function up()
{
    Schema::create('team_messages', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('team_id');
        $table->integer('message_id');
    });
}

The relationship inside the team model would be:

public function messages()
{
    return $this->belongsToMany('App\Message');
}

When it comes to the user to message relationship what would be the best option if the user can send a message and has the abilty to list messages sent too them from the team?

Upvotes: 0

Views: 435

Answers (1)

DOLWIN DAVIS
DOLWIN DAVIS

Reputation: 91

if you are using same table for both type of messages u can use polymorphism in eloquent

Schema::create('messages', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('message');
        $table->morphs('sender');
        $table->morphs('recipient');
        $table->timestamps();
    });

this will create sender_id,sender_type,reciever_id,reciever_type and u don't need a pivot table

https://laravel.com/docs/8.x/migrations#column-method-morphs

for relationships u can do like

in Message.php

public function sender()
{
     return $this->morphTo();
}

public function recipient()
{
     return $this->morphTo();
}

in team.php

 public function sendMessages()
 {
     return $this->morphMany(Message::class, 'sender');
 }

 public function recievedMessages()
 {
     return $this->morphMany(Message::class, 'recipient');
 }

in user.php

 public function sendMessages()
 {
     return $this->morphMany(Message::class, 'sender');
 }

 public function recievedMessages()
 {
     return $this->morphMany(Message::class, 'recipient');
 }

for more info check https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-polymorphic-relations

MessageController.php

function create(){
  //if team was sending
  
   $team->sendMessage()->create([
    
    'message' => "",
    'sender_id => $user->id,
    'sender_type' => base_class($user)

    ])  

}

Upvotes: 0

Related Questions