[英]Relationships returning wrong/null data (Laravel 5.2)
得到了One To Many relationship
與domain_hosts_table
, server_hosts_table
和systems_table
有domain_hosts_table
One To Many relationship
的domain table
。 到現在為止還挺好。
調用表數據:
$domains = Domain::with('domain_host', 'server_host', 'system')->get();
領域模型:
public function domain_host()
{
return $this->hasOne('App\DomainHost', 'id');
}
public function server_host()
{
return $this->hasOne('App\ServerHost', 'id');
}
public function system()
{
return $this->hasOne('App\System', 'id');
}
DomainHost , ServerHost , 系統模型:
public function domains()
{
return $this->hasMany('App\Domain');
}
域表:
到現在為止還挺好。
讓我們看一下該表在被foreached
返回的foreached
。
前兩行應該相同(基於其ID),並且前兩行之后的所有行都為空。
(獲取的數據的dd
,請注意第4個對象為空,第1個對象實際有數據)。
在定義我的關系時必須定義另一個參數:
public function domain_host()
{
return $this->hasOne('App\DomainHost', 'id', 'domain_host_id');
}
public function server_host()
{
return $this->hasOne('App\ServerHost', 'id', 'server_host_id');
}
public function system()
{
return $this->hasOne('App\System', 'id', 'system_id');
}
它在另一張表中查找當前行的ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.