2014-04-02 35 views
1

我曾经在数据库四个表:这两张表在laravel中有什么样的关系?

  1. 用户(用于存储用户详情)
  2. 会话(用于存储会话ID)。
  3. conversationsmember(用于存储会话构件)
  4. 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(); 

conversationsreply migrati ons

$table->increments('id'); 

$table->integer('conversation_id'); 
$table->integer('user_id'); 

$table->timestamps(); 

现在在用户模型中,我需要定义用户表和会话表之间的关系。由于它们不是直接相连的,我使用了ManyThrough关系。

..app /模型/ user.php的

... 
    public function conversations() 
     { 
      return $this->hasManyThrough('Conversation', 'ConversationsMember', 'conversation_id', 'id'); 
     } 
... 

当我试图使用它,它显示一个空白阵列。

回答

0

我会尝试一个belongsToMany关系,其中conversationsmembers是您的数据透视表。

public function conversations() 
{ 
    return $this->belongsToMany('Conversation', 'conversationsmembers'); 
} 

你也可以定义你的谈话模式的关系的逆:

public function users() 
{ 
    return $this->belongsToMany('User', 'conversationsmembers'); 
} 

我有点迷惑有关迁移的,所以我不知道这是你想要的。