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