1

我正在尝试创建多参与者消息传递系统。这是我在这里找到的数据库设计:消息系统数据库模式

Conversation
------------
id

Messages
--------
id
conversation_id
from_id
subject
message
from_timestamp

Participants
------------
conversation_id
user_id
last_read_timestamp

你能帮我描述一下这些模型之间的雄辩关系吗?我的意思是:http: //laravel.com/docs/eloquent#relationships

4

1 回答 1

4
class Conversation extends Eloquent
{
    public function messages()
    {
        return $this->hasMany('Message');
    }

    public function participants()
    {
        return $this->hasMany('Participant');
    }
}

class Message extends Eloquent
{
    public function conversation()
    {
        return $this->belongsTo('Conversation');
    }

    public function from()
    {
        return $this->belongsTo('User', 'from_id');
    }
}

class Participant extends Eloquent
{
    public function conversations()
    {
        return $this->belongsTo('Conversation');
    }

    public function user()
    {
        return $this->belongsTo('User');
    }
}

// Messages 
foreach($conversation->messages as $message)
{
    echo 'From: '.$message->from->username."<br />";  // Taking a guess here, depends on your user model.
    echo 'Subject: '.$message->subject."<br />";
    echo 'Message: '.$message->message."<br />";
    echo 'Timestamp: '.$message->from_timestamp."<br />";
    echo '<hr />';
}

// Participants
foreach($conversation->participants as $participant)
{
    echo 'User: '.$participant->user->username;
}
于 2013-10-11T21:28:21.330 回答