2016-06-30 49 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。