[英]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.