Laravel 3路关系

时间:2017-03-27 09:30:03

标签: laravel relationship

user
id *
name

reviewers
type_id *
user_id *
user_id

reviewer_types
id *
name

我如何在Laravel中创建评论者关系? 结果应该是:

reviewers
type_id: 1 
user_id: 1
user_id: 2

1 个答案:

答案 0 :(得分:0)

试试这个

class Reviewer
{
    public function user1()
    {
        return $this->belongsTo(User::class, 'user_id_1');
    }

    public function user2()
    {
        return $this->belongsTo(User::class, 'user_id_2');
    }

    public function reviewType()
    {
        return $this->belongsTo(ReviewType::class, 'type_id');
    }
}

Reviewer::with('user1')->with('user2')->with('reviewType')->first();

希望这有帮助