繁体   English   中英

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

[英]Relationships returning wrong/null data (Laravel 5.2)

得到了One To Many relationshipdomain_hosts_tableserver_hosts_tablesystems_tabledomain_hosts_table One To Many relationshipdomain 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');
}

DomainHostServerHost系统模型:

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.

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