简体   繁体   中英

Query relations ship with 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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