2016-06-30 48 views
0

得到一個domain table具有帶有domain_hosts_tableserver_hosts_tablesystems_table一個One To Many relationship。到現在爲止還挺好。關係返回錯誤/空數據(Laravel 5.2)

調用表中的數據:

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

DomainHostSERVERHOST系統型號:

public function domains() 
{ 
    return $this->hasMany('App\Domain'); 
} 

表:

enter image description here

到目前爲止好。

讓我們來看看這個特定的表返回的是foreached

enter image description here

第2行應該是(基礎上其ID)相同,前2後的所有行都只是空的。

dd的取數據,注意在第4個對象空關係,第1個對象其實有數據)。

enter image description here

回答

0

當初定義我的關係時,定義另一個參數:

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。