我在数据库中有四个表:
- 用户
- (用于存储用户详细信息) 对话
- (用于存储对话 ID)。 对话成员
- (用于存储对话成员) 对话回复
- (用于存储对话回复)
用户将有许多对话,每个对话都有其成员和回复。
以下是详细信息:
用户迁移:
$table->increments('id');
$table->string('name', 32);
$table->string('username', 32);
$table->string('email', 320);
$table->string('password', 64);
$table->timestamps();
对话迁移:
$table->increments('id');
$table->timestamps();
对话成员迁移:
$table->increments('id');
$table->integer('conversation_id');
$table->integer('user_id');
$table->timestamps();
对话回复迁移
$table->increments('id');
$table->integer('conversation_id');
$table->integer('user_id');
$table->timestamps();
现在在用户模型中,我需要定义用户表和对话表之间的关系。由于它们没有直接连接,我使用了hasManyThrough关系。
..应用/模型/用户.php
...
public function conversations()
{
return $this->hasManyThrough('Conversation', 'ConversationsMember', 'conversation_id', 'id');
}
...
当我尝试使用它时,它显示一个空白数组。
我会尝试一种属于多的关系,其中对话成员是你的数据透视表。
public function conversations()
{
return $this->belongsToMany('Conversation', 'conversationsmembers');
}
您可能还想在对话模型中定义关系的反转:
public function users()
{
return $this->belongsToMany('User', 'conversationsmembers');
}
我对你的迁移有点困惑,所以我不确定这就是你想要的。