简体   繁体   English

laravel 5中两个表之间的聚合关系(两个或多个)

[英]Aggregation relationships (two or more) between two tables in laravel 5

I am developing a web app using laravel 5. Now i am having a issue below: 我正在使用laravel 5开发Web应用程序。现在,我遇到以下问题:

  • I have two table users and feedbacks 我有两个表用户和反馈

    • users: id, username, password 用户:ID,用户名,密码

    • feedbacks: id, provider, receiver, reviewer, content 反馈:ID,提供者,接收者,审阅者,内容

  • I want to provider, receiver and reviewer is a user. 我要提供者,接收者和审阅者是用户。 Meaning i will storage user'id into provider, receiver and reviewer. 这意味着我会将用户ID存储到提供者,接收者和审阅者中。

  • In laravel, i only can setup a relationship for model User like: $this->belongsTo('User', 'provider', 'id') 在laravel中,我只能为模型用户设置一个关系,例如: $this->belongsTo('User', 'provider', 'id')

  • And i want to receiver and reviewer also have belongsTo relationship 而且我希望接收者和审阅者也具有belongsTo关系

  • I thought creating the third table for many to many relationship, but if i do it, i must create two tables for receiver and reviewer. 我以为为多对多关系创建第三个表,但是如果这样做,我必须为接收者和审阅者创建两个表。

I need helping to solve this issue without using more queries on controller, thanks for reading. 我需要帮助解决此问题,而无需在控制器上使用更多查询,谢谢阅读。

in your feedbacks Model create the following relations: 在您的反馈模型中创建以下关系:

public function provider()
{
    $this->belongsTo('User', 'provider', 'id');
}

public function reviewer()
{
    $this->belongsTo('User', 'reviewer', 'id');
}

public function receiver()
{
    $this->belongsTo('User', 'receiver', 'id');
}

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

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