Apr 8, 2022
0
Level 3
Chat database and model setup
Hi all,
I have a little hard time finding out how to structure my Chat database and models, which i hope someone can look at and give some feedback on ✋
So first my migrations are
Schema::create('chat_conversations', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
Schema::create('chat_participants', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('conversation_id');
$table->morphs('sender');
$table->timestamps();
$table->foreign('conversation_id')->references('id')->on('chat_conversations');
});
Schema::create('chat_messages', function (Blueprint $table) {
$table->id();
$table->string('message');
$table->unsignedBigInteger('conversation_id');
$table->unsignedBigInteger('participant_id');
$table->string('type');
$table->timestamps();
$table->foreign('conversation_id')->references('id')->on('chat_conversations');
$table->foreign('participant_id')->references('id')->on('chat_participants');
});
Schema::create('chat_notifications', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('chat_message_id');
$table->morphs('sender');
$table->unsignedBigInteger('conversation_id');
$table->unsignedBigInteger('participant_id');
$table->boolean('is_seen')->default(false);
$table->timestamps();
$table->foreign('chat_message_id')->references('id')->on('chat_messages');
$table->foreign('conversation_id')->references('id')->on('chat_conversations');
$table->foreign('participant_id')->references('id')->on('chat_participants');
});
Models
class ChatConversation extends Model
{
use HasFactory;
protected $table = 'chat_conversations';
protected $fillable = [
'id',
];
public function participants()
{
return $this->hasMany(ChatParticipant::class, 'conversation_id');
}
public function messages()
{
return $this->hasMany(ChatMessage::class, 'conversation_id');
}
}
class ChatParticipant extends Model
{
use HasFactory;
protected $database = 'chat_participants';
protected $fillable = [
'id',
'conversation_id',
'sender_id',
'sender_type',
];
public function conversation()
{
return $this->belongsTo(ChatConversation::class);
}
public function sender()
{
return $this->morphTo();
}
}
class ChatMessage extends Model
{
use HasFactory;
protected $database = 'chat_messages';
protected $fillable = [
'id',
'message',
'conversation_id',
'participant_id',
'type', // This should be text,audio,video
];
public function conversation()
{
return $this->belongsTo(ChatConversation::class, 'conversation_id');
}
public function sender()
{
return $this->morphTo();
}
}
class ChatNotifications extends Model
{
use HasFactory;
protected $database = 'chat_notifications';
protected $fillable = [
'id',
'chat_message_id',
'sender_id',
'sender_type',
'conversation_id',
'participant_id',
'is_seen',
];
public function conversation()
{
return $this->belongsTo(ChatConversation::class);
}
}
class User extends Authenticatable {
public function messages()
{
return $this->morphMany(ChatMessage::class, 'sender');
}
public function conversations()
{
return $this->hasMany(ChatConversation::class);
}
public function participants()
{
return $this->belongsToMany(ChatParticipant::class);
}
}
class Client extends Authenticatable {
public function messages()
{
return $this->morphMany(ChatMessage::class, 'sender');
}
public function conversations()
{
return $this->hasMany(ChatConversation::class);
}
public function participants()
{
return $this->belongsToMany(ChatParticipant::class);
}
}
My issue is that I want to get the name of the user/client that has created the message. Something like this
$chatConversation = ChatConversation::first();
dd($chatConversation->messages->first()->sender);
Please or to participate in this conversation.