在laravel 4中定义关系时陷入困境

在laravel 4中定义关系时陷入困境,laravel,Laravel,我试图通过创建一个消息传递应用程序来理解laravel。用户应该能够互相发送消息。我使用核心php开发了一个类似的应用程序 我已经完成了登录/身份验证和迁移,现在我只能在模型中定义关系 我使用迁移创建了3个表: 使用者 对话 对话与答复 这是的模式: 用户表(用于存储用户详细信息) 对话表(用于存储用户之间的对话) 对话\回复表(用于存储对话文本) 现在,我尝试将模型中的关系定义为: Usermodel与Conversation和ConversationReplymodel都有很多关系 C

我试图通过创建一个消息传递应用程序来理解laravel。用户应该能够互相发送消息。我使用核心php开发了一个类似的应用程序

我已经完成了登录/身份验证和迁移,现在我只能在模型中定义关系

我使用迁移创建了3个表:

  • 使用者
  • 对话
  • 对话与答复
  • 这是的模式:

  • 用户表(用于存储用户详细信息)

  • 对话表(用于存储用户之间的对话)

  • 对话\回复表(用于存储对话文本)

  • 现在,我尝试将模型中的关系定义为:

  • User
    model与
    Conversation
    ConversationReply
    model都有很多关系
  • Conversation
    将与
    用户
    模型具有归属关系,并与ConversationReply模型具有许多关系
  • ConversationReply
    model将与
    User
    Conversation
    model都有归属关系
  • 现在,我被困在第一个模型(用户)中定义关系,无法继续,因为我需要定义本地键和外键,但我无法这样做,因为对话表将有2个外键(2个用户),我只能定义一个外键


    编辑:一个对话中应该只有两个成员,两个用户应该只有一个对话(如facebook)。他们的新信息应该添加到他们的旧对话中。在conversations表中,ip是将启动对话的用户的ip地址,在conversations\u reply表中,ip是用户的相应ip

    您的抽象中似乎有一点缺陷。实际上,您已经将user1和user2设计为会话实体的属性,但它们不是属性。另外,会话的IP是多少

    会话的属性可以是主题、开始时间、结束时间、消息量等等

    然后一个对话就有了成员。不是两个而是很多。因此,您可以创建一个实体/模型ConversationMembers来连接用户和对话:

    对话\u成员表:

    $table->increments('id');
    
    $table->integer('conversation_id');
    $table->integer('user_id');
    $table->string('ip');
    $table->string('nickname');
    
    public function members()
    {
        return $this->hasMany('ConversationMember');
    }
    
    public function messages()
    {
        return $this->hasMany('ConversationReply');
    }
    
    public function user()
    {
        return $this->belongsTo('User');
    }
    
    public function conversation()
    {
        return $this->belongsTo('Conversation');
    }
    
    public function conversations()
    {
        return $this->hasManyThrough('Conversation', 'ConversationMember');
    }
    
    public function replies()
    {
        return $this->hasMany('ConversationReply');
    }
    
    并相应地更改对话表:

    $table->increments('id');
    
    $table->boolean('public);
    // other attributes you're interested in
    $table->timestamps();
    
    现在,您可以在模型上定义关系:

    对话:

    $table->increments('id');
    
    $table->integer('conversation_id');
    $table->integer('user_id');
    $table->string('ip');
    $table->string('nickname');
    
    public function members()
    {
        return $this->hasMany('ConversationMember');
    }
    
    public function messages()
    {
        return $this->hasMany('ConversationReply');
    }
    
    public function user()
    {
        return $this->belongsTo('User');
    }
    
    public function conversation()
    {
        return $this->belongsTo('Conversation');
    }
    
    public function conversations()
    {
        return $this->hasManyThrough('Conversation', 'ConversationMember');
    }
    
    public function replies()
    {
        return $this->hasMany('ConversationReply');
    }
    
    对话成员:

    $table->increments('id');
    
    $table->integer('conversation_id');
    $table->integer('user_id');
    $table->string('ip');
    $table->string('nickname');
    
    public function members()
    {
        return $this->hasMany('ConversationMember');
    }
    
    public function messages()
    {
        return $this->hasMany('ConversationReply');
    }
    
    public function user()
    {
        return $this->belongsTo('User');
    }
    
    public function conversation()
    {
        return $this->belongsTo('Conversation');
    }
    
    public function conversations()
    {
        return $this->hasManyThrough('Conversation', 'ConversationMember');
    }
    
    public function replies()
    {
        return $this->hasMany('ConversationReply');
    }
    
    用户:

    $table->increments('id');
    
    $table->integer('conversation_id');
    $table->integer('user_id');
    $table->string('ip');
    $table->string('nickname');
    
    public function members()
    {
        return $this->hasMany('ConversationMember');
    }
    
    public function messages()
    {
        return $this->hasMany('ConversationReply');
    }
    
    public function user()
    {
        return $this->belongsTo('User');
    }
    
    public function conversation()
    {
        return $this->belongsTo('Conversation');
    }
    
    public function conversations()
    {
        return $this->hasManyThrough('Conversation', 'ConversationMember');
    }
    
    public function replies()
    {
        return $this->hasMany('ConversationReply');
    }
    

    我希望这能有所帮助。

    Edit:一个对话中应该只有两个成员,两个用户应该只有一个对话(如facebook)。他们的新信息应该添加到他们的旧对话中。在conversations表中,ip是将启动对话的用户的ip地址,在conversations_reply表中,ip是会话的相应ipuser@user170654好的,实际上应该有多少成员并不重要,因为这在数据抽象级别上并不重要。多意味着一个或多个。为了表明某个特定的用户已经开始了对话,我认为决定设置哪个IP不是一个好主意。相反,您应该只添加另一个属性,如
    $table->boolean('initiator')到您的对话成员表-因为这实际上是对话成员的一个属性。