【问题标题】:Laravel relationship with nested columns?Laravel与嵌套列的关系?
【发布时间】:2017-05-25 05:10:39
【问题描述】:

我在使用Laravel和mongodb,对laravel eloquent关系了解不多,目前我的集合结构如下

collection name:general_details

{
    "id": 01,
    "personal_details":[
        [
            "emp_id":10,
            "blood_group":"B+ve"
        ],
        [
            "emp_id":11,
            "blood_group":"B+ve"
        ]
    ]
}

collection name:employee_details

{
    "emp_id":10,
    "emp_name":"Alex"
},
{
    "emp_id":11,
    "emp_name":"Ramesh"
}

我想在“emp_id”的两个集合之间建立雄辩的关系,请提出任何解决方案?

【问题讨论】:

  • 两个表的型号名称是什么?
  • 两者的模型名称,GenDetails,EmpDetails

标签: mongodb laravel


【解决方案1】:

在 GenDetails 模型中

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

在 EmpDetails 模型中

public function genDetails(){
    return $this->belongsTo('App\GenDetails');
}

这里是 GenDetails 和 EmpDetails 模型之间的一对一关系。因此,请从 laravel documentation 了解更多关于 Eloquent 关系的信息。

【讨论】:

  • 不,我的收藏有嵌套列,这就是问题
【解决方案2】:

GenDetail Model中放入这个关系

public function empDetails(){
    return $this->hasMany('App\EmpDetails','emp_id','personal_details.emp_id');
}

我认为这种关系绝对适合你。

【讨论】:

    猜你喜欢
    • 2021-08-05
    • 2017-10-17
    • 2018-05-26
    • 2014-10-27
    • 2021-07-19
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多