首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用laravel-mongodb查询关系

使用laravel-mongodb查询关系
EN

Stack Overflow用户
提问于 2015-06-27 05:37:13
回答 1查看 248关注 0票数 0

我在Laravel有两个模型

代码语言:javascript
复制
class Customer extends Moloquent{

  protected $collection = 'Customers';
  protected $guarded = [];

  public function maritalStatus()
  {
    return $this->hasOne('App\Models\MaritalStatus', 'maritalStatus_id', '_id');
  }

}


class MaritalStatus extends Moloquent{

  protected $collection = 'MaritalStatus';
  protected $guarded = [];

  public function customer() {
    return $this->belongsTo('App\Models\Customer');
  }

}

当我进行查询时

代码语言:javascript
复制
$customers = Customer::with('maritalStatus')->get();

结果是一个完整的客户条目,但在marital_status中始终为空

代码语言:javascript
复制
> _id: "558daaf95129a452020043b7" address: "Solanda Sector 1" cantonCode: "1" cantonName: "Quito" cellphones: [] created_at:
> "2015-06-26 19:41:45" customerSex: "M" customerType: "N" emails: []
> identification: "1715339444" incomeSourceCode: "B" incomeSourceName:
> "Empleado Público" isPassport: "0" maritalStatusCode: ""
> maritalStatusName: "Soltero" maritalStatus_id:
> "558d7c545129a45202004355"
> **marital_status: null** names: "Bryan Alexis" parishCode: "8" parishName: "Chillogallo" provinceCode: "17" provinceName: "Pichincha"
> surnames: "Guamba Chamorro" telephones: [] updated_at: "2015-06-26
> 19:41:45" user_id: "558d7c5e5129a452020043aa"

我尝试了几次不同的选择,但我不知道发生了什么,我疯了!请帮帮我!

EN

回答 1

Stack Overflow用户

发布于 2015-07-09 21:49:59

这真的是一对一的关系吗?假设婚姻状况包含“单身”、“结婚”、“离婚”、“寡居”等,这将是一对多的关系。

代码语言:javascript
复制
class Customer extends Moloquent
{
    protected $collection = 'Customers';
    protected $guarded = [];

    public function maritalStatus()
    {
        return $this->belongsTo('App\Models\MaritalStatus', 'maritalStatus_id', '_id');
    }

}

class MaritalStatus extends Moloquent
{
    protected $collection = 'MaritalStatus';
    protected $guarded = [];

    public function customer() 
    {
        return $this->hasMany('App\Models\Customer', 'maritalStatus_id', '_id');
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31082529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档