【发布时间】:2014-05-13 05:28:07
【问题描述】:
我在数据库中有四个表:
- users(用于存储用户详细信息)
- 对话(用于存储对话 ID)。
- conversationsmember(用于存储会话成员)
- conversationsreply(用于存储对话回复)
一个用户会有很多对话,每个对话都有自己的成员和回复。
详情如下:
用户迁移:
$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();
conversationsmembers 迁移:
$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 关系。
..app/models/User.php
...
public function conversations()
{
return $this->hasManyThrough('Conversation', 'ConversationsMember', 'conversation_id', 'id');
}
...
当我尝试使用它时,它显示一个空白数组。
【问题讨论】:
标签: php laravel relationship