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

报告相同问题?

悬赏问题

  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统