【问题标题】:How to declare Multiple hasOne relation in laravel 5.2如何在 laravel 5.2 中声明多个 hasOne 关系
【发布时间】:2016-02-10 22:39:09
【问题描述】:

您好,我的数据库中有 7 个表,

所有 6 个表都有来自用户表的 user_id,

在用户模型中,我声明 hasOne 关系如下

 public function profiledetailsHasOne()
{
        return $this->hasOne('App\Profiledetails');
}


public function educationHasOne()
{
        return $this->hasOne('App\Education');
}
public function occupationHasOne()
{
        return $this->hasOne('App\Occupation');
}

public function maritalHasOne()
{
        return $this->hasOne('App\Marital');
}
public function parentHasOne(){
    return $this->hasOne('App\Parent');
 }

我想知道除了使用这种集群方式之外,还有其他方法吗?

提前致谢

【问题讨论】:

    标签: model eloquent laravel-5.2 has-one


    【解决方案1】:

    我不会对结构发表评论,但您的做法是正确的。

    我会从函数中删除 HasOne 单词,以便在访问如下关系时它们读起来更好。

    $user->profileDetails();
    $user->education();
    

    而不是

    $user->profiledetailsHasOne();
    $user->educationHasOne();
    

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 1970-01-01
      • 2020-01-18
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 2017-04-21
      • 2016-03-17
      相关资源
      最近更新 更多