繁体   English   中英

查询关系与laravel-mongodb一起提供

[英]Query relations ship with laravel-mongodb

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

}

当我查询时

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

结果是完整的客户输入,但在marital_status中始终为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"

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

这真的是一对一的关系吗? 假设婚姻状况包含“单身”,“已婚”,“离婚”,“丧偶”之类的东西,那将是一对多的关系。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM