drfu29983 2015-06-26 21:37
浏览 226

与laravel-mongodb查询关系

I Have two models in Laravel

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');
  }

}

And when I make a query

$customers = Customer::with('maritalStatus')->get();

the result is a complete customer entry but in marital_status is always NULL

> _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"

I try several times with diferent options but i dont know what happend, I am crazy!, please help me!

  • 写回答

1条回答 默认 最新

  • doudou130216 2015-07-09 13:49
    关注

    Is this really a one-to-one relationship? Assuming that marital status contains things like "single", "married", "divorced", "widowed" and such, this would be a one-to-many relationship.

    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');
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配