Laravel 5 与Laravel 5.4的雄辩关系

Laravel 5 与Laravel 5.4的雄辩关系,laravel-5,Laravel 5,我有以下模型和相应的表格: 用户(用户)、用户配置文件(用户配置文件)、审核(审核) 模式 table: users user_id (primary key) first_name last_name table: user_profiles user_id (foreign key) country phone table: reviews reviewer_id (the user who posted the review) user_id (foreign key) body

我有以下模型和相应的表格:

用户(用户)、用户配置文件(用户配置文件)、审核(审核)

模式

table: users

user_id (primary key)
first_name
last_name

table: user_profiles
user_id (foreign key)
country
phone

table: reviews
reviewer_id (the user who posted the review)
user_id (foreign key)
body
我使用以下命令查询数据库:

$user = User::with('profile', 'review')->find(Auth::User()->user_id);
用户模型的一部分

public function profile(){
    return $this->hasOne(UserProfile::class, 'user_id');
}    

public function review(){
    return $this->hasOne(Review::class, 'user_id');
}
 public function users() {
    return $this->belongsTo(User::class, 'user_id');
 }
 public function users() {
    return $this->belongsTo(User::class, 'user_id');
 }
用户配置文件模型的一部分

public function profile(){
    return $this->hasOne(UserProfile::class, 'user_id');
}    

public function review(){
    return $this->hasOne(Review::class, 'user_id');
}
 public function users() {
    return $this->belongsTo(User::class, 'user_id');
 }
 public function users() {
    return $this->belongsTo(User::class, 'user_id');
 }
审查模型的一部分

public function profile(){
    return $this->hasOne(UserProfile::class, 'user_id');
}    

public function review(){
    return $this->hasOne(Review::class, 'user_id');
}
 public function users() {
    return $this->belongsTo(User::class, 'user_id');
 }
 public function users() {
    return $this->belongsTo(User::class, 'user_id');
 }

如何使用
users
表中的
reviewer\u id
访问
reviewer
的详细信息作为本问题所讨论内容的补充,我认为OP主要关注的是一个表中有两个外键,并且具有相同的表。
user\u id
reviewer\u id
都是链接到
users
表的外键

关于Laravel人际关系,需要了解的一件重要事情是,人际关系的名称不是硬性的,它可以是任何东西,只要对你有意义。相关对象将在属性中可用,其确切名称为方法

class User extends Model {

     public function review() {
         return $this->hasOne(Review::class);
     }
}
用户评论将在
user::find(1)->review上提供

class Review extends Model {

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

     public function reviewer() {
         return $this->belongsTo(User::class, 'reviewer_id');    
     }
}
通过查看,您将能够访问两个用户:

Review::find(1)->owner; // This is the relationship with user_id column
Review::find(1)->reviewer; // This is the relationship with reviewer_id column

作为对这个问题所讨论内容的总结,我认为OP的主要关注点是关于一个表,该表有两个外键,并且具有相同的表。
user\u id
reviewer\u id
都是链接到
users
表的外键

关于Laravel人际关系,需要了解的一件重要事情是,人际关系的名称不是硬性的,它可以是任何东西,只要对你有意义。相关对象将在属性中可用,其确切名称为方法

class User extends Model {

     public function review() {
         return $this->hasOne(Review::class);
     }
}
用户评论将在
user::find(1)->review上提供

class Review extends Model {

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

     public function reviewer() {
         return $this->belongsTo(User::class, 'reviewer_id');    
     }
}
通过查看,您将能够访问两个用户:

Review::find(1)->owner; // This is the relationship with user_id column
Review::find(1)->reviewer; // This is the relationship with reviewer_id column

你们的关系不是多对多,而是多对一。用户应该
$this->haveMany()
和用户配置文件应该
$this->belongsTo()
您的问题很难理解
审阅者id
用户id
之间的区别。看起来它们是一样的。
reviewer\u id
实际上持有一个
user\u id
数据,但是对于在审阅模型中编写审阅的用户,您应该为
user()
提供一个方法,并为
user\u id
的用户提供一个方法,为
reviewer()提供一个方法
with belongs给具有
reviewer\u id
的用户。然后,从$user变量do
$user->review->reviewer
应该通过
reviewer\u id
@MarcoAurélioDeleu向您提供该用户。谢谢,如您所愿,您的关系不是多对多,而是多对一。用户应该
$this->haveMany()
和用户配置文件应该
$this->belongsTo()
您的问题很难理解
审阅者id
用户id
之间的区别。看起来它们是一样的。
reviewer\u id
实际上持有一个
user\u id
数据,但是对于在审阅模型中编写审阅的用户,您应该为
user()
提供一个方法,并为
user\u id
的用户提供一个方法,为
reviewer()提供一个方法
with belongs给具有
reviewer\u id
的用户。然后,从$user变量do
$user->review->reviewer
应该通过
reviewer\u id
@MarcoAurélioDeleu向您提供用户感谢,按需工作