【发布时间】:2016-12-11 13:46:19
【问题描述】:
我正在实现没有任何包的简单私人消息应用程序。
消息表有以下列 id, from_id, to_id, message, created_at
我希望用户显示如下示例的消息-
用户1:嗨 用户2:你好
用户1:你好吗? 用户1:怎么了? 用户 2:很好。
【问题讨论】:
我正在实现没有任何包的简单私人消息应用程序。
消息表有以下列 id, from_id, to_id, message, created_at
我希望用户显示如下示例的消息-
用户1:嗨 用户2:你好
用户1:你好吗? 用户1:怎么了? 用户 2:很好。
【问题讨论】:
这是many to many 关系。您应该在users 表中创建带有两个指向id 的键的数据透视表:
Schema::create('messages_pivot', function (Blueprint $table) {
$table->increments('id');
$table->integer('from_id')->unsigned();
$table->integer('to_id')->unsigned();
$table->string('message');
$table->timestamps();
});
Schema::table('messages_pivot', function (Blueprint $table) {
$table->foreign('from_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('to_id')->references('id')->on('users')->onDelete('cascade');
});
您可以使用relations 处理此数据透视表,也可以为其创建模型。
【讨论】:
创建两个表Conversations和Messages在用户之间使用conversation_id(如果存在),然后在messages表中添加消息并使用user_id存储谁发送了消息。
class CreateMessagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->text('message');
$table->boolean('is_seen')->default(0);
$table->boolean('deleted_from_sender')->default(0);
$table->boolean('deleted_from_receiver')->default(0);
$table->integer('user_id');
$table->integer('conversation_id');
$table->timestamps();
});
}
class CreateConversationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('conversations', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_one');
$table->integer('user_two');
$table->boolean('status')->default(1);
$table->timestamps();
});
}
【讨论】: