[英]Relationships returning wrong/null data (Laravel 5.2)
Got a domain table
which has a One To Many relationship
with domain_hosts_table
, server_hosts_table
and systems_table
. 得到了One To Many relationship
与domain_hosts_table
, server_hosts_table
和systems_table
有domain_hosts_table
One To Many relationship
的domain table
。 So far so good. 到现在为止还挺好。
Calling the table data: 调用表数据:
$domains = Domain::with('domain_host', 'server_host', 'system')->get();
Domain model : 领域模型:
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 , System model : DomainHost , ServerHost , 系统模型:
public function domains()
{
return $this->hasMany('App\Domain');
}
Domains table : 域表:
So far so good. 到现在为止还挺好。
Let's take a look at what this particular table returns while being foreached
. 让我们看一下该表在被foreached
返回的foreached
。
The first 2 rows should be the same (basing on their IDs), and all rows after the first 2 are just empty. 前两行应该相同(基于其ID),并且前两行之后的所有行都为空。
( dd
of the fetched data, notice the relations being empty at 4th object, 1st object actually has data). (获取的数据的dd
,请注意第4个对象为空,第1个对象实际有数据)。
Had to define another parameter when defining my relationships: 在定义我的关系时必须定义另一个参数:
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');
}
It was looking for the ID of the current row in the other table. 它在另一张表中查找当前行的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.