I am making a private(one to one) chat app in Laravel 5.7. The problem is with schema I think. If user1 is logged in and he creates a new conversation with user2. That would be created with conversation_id=1. Next time when user2 is logged in lets assume he will be finding his ID in sender column and he won't find his ID in sender_column, new conversation will be created which shouldn't be created because for creating the new conversation we have to check in sender and receiver column both.
Here is my schema
=======User Table========
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
=============Chat Table=============
Schema::create('chats', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user1_id')->nullable();
$table->foreign('user1_id')->references('id')->on('users');
$table->unsignedInteger('user2_id')->nullable();
$table->foreign('user2_id')->references('id')->on('users');
$table->timestamps();
});
===============Messages Table================
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('chat_id')->nullable();
$table->foreign('chat_id')->references('id')->on('chats');
$table->text('messages');
$table->timestamps();
});
Maybe you understand it by this.
Users Chats
id name chat_id user1_id user2_id
1 Mark 1 1 2
2 David 2 2 1
3 Henzard 3 2 3
4 3 2
Please suggest me a better solution.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire