簡體   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