繁体   English   中英

这两个表在laravel中将具有什么样的关系?

[英]What type of relationship these two tables will have in laravel?

我在数据库中有四个表:

  1. 用户(用于存储用户详细信息)
  2. 对话(用于存储对话ID)。
  3. 对话成员(用于存储对话成员)
  4. 对话回复(用于存储对话回复)

一个用户将有很多对话,每个对话将有其成员和答复。

详细信息如下:

用户迁移:

$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关系。

..app /模型/ user.php的

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

当我尝试使用它时,它显示一个空白数组。

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

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

您可能还想在“会话”模型中定义关系的反函数:

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

我对您的迁移感到有些困惑,所以我不确定那不是您想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM