简体   繁体   English

关系返回错误/空数据(Laravel 5.2)

[英]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 relationshipdomain_hosts_tableserver_hosts_tablesystems_tabledomain_hosts_table One To Many relationshipdomain 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 : DomainHostServerHost系统模型:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM