Laravel 4 Relationship: a message belong to two user

牧云@^-^@ 提交于 2019-12-03 21:53:57

Check this out: http://laravel.com/docs/eloquent#many-to-many

Basically you need a table structure like this

Users: id | username

Messages: id | from | content

user_messages: user_id | message_id


You can define your models like this

class User extends Eloquent {

    public function messages()
    {
        return $this->belongsToMany('Message');
    }

    public function sent_messages()
    {
        return $this->hasMany('Messages', 'from');
    }

}

class Message extends Eloquent {

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

    public function to()
    {
        return $this->belongsToMany('User');
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!