简体   繁体   English

Laravel集合第二结果集关系始终为null

[英]Laravel collection 2nd result set relationship always null

I have a query where I'm returning a one to many relationship I'm expecting two results both with their relationship. 我有一个查询,我正在返回一对多关系,我期望两个关系都可以得到两个结果。 The first result returns correctly but after the first result, I always get a NULL value for my relationship data for every result after the first. 第一个结果正确返回,但是在第一个结果之后,对于第一个结果之后的每个结果,我总是为我的关系数据获得NULL值。

If I delete the 1st record in my database the 2nd then becomes the first and it returns correctly. 如果删除数据库中的第一条记录,那么第二条记录将成为第一条记录并正确返回。

This is my query - 这是我的查询-

$groups = DataGroup::where('post_id', $post->id)->with('data.dataType')->get();

DataGroup relationship 数据组关系

public function data()
{
    return $this->hasMany('App\Data', 'data_group_id');
}

Data relationships 数据关系

public function dataType()
{
    return $this->belongsTo('App\DataTypes', 'id');
}

public function dataGroup()
{
    return $this->belongsTo('App\DataGroup', 'id');
}

DataType relationship 数据类型关系

public function data()
{
    return $this->hasMany('App\Data', 'data_types_id');
}

The result set (stripped out other information for easier reading) 结果集(删除了其他信息以方便阅读)

Collection {#259 ▼
  #items: array:2 [▼
    0 => Data {#272 ▼
      #fillable: array:5 [▶]
      #attributes: array:8 [▶]
      #original: array:8 [▶]
      #relations: array:1 [▼
        "dataType" => DataTypes {#271 ▶}
      ]
    }
    1 => Data {#268 ▼
      #fillable: array:5 [▶]
      #attributes: array:8 [▶]
      #original: array:8 [▶]
      #relations: array:1 [▼
        "dataType" => null
      ]

Data Groups Table 数据组表

---------------------
| id | post_id      |
--------------------
| 1  | 2            |
--------------------

Data Types table 数据类型表

---------------------
| id | label | name |
--------------------
| 1  | Text  | text |
--------------------

Data table 数据表

------------------------------------------------------------
| id | data_types_id | data_group_id | field_label | value |
------------------------------------------------------------
| 1  |  1            | 1             | Title       | NULL  |
------------------------------------------------------------
| 2  |  1            | 1             | Sub Heading | NULL  |

I believe if you change: 我相信如果您改变:

public function dataType()
{
    return $this->belongsTo('App\DataTypes', 'id');
}

into: 成:

public function dataType()
{
    return $this->belongsTo('App\DataTypes', 'data_type_id');
}

that should resolve the problem, it is currently trying to refer to a datatype with id 2(which is the id of data 2), which does not exist. 应该可以解决该问题的方法,它目前正在尝试引用ID 2(即数据2的ID)不存在的数据类型。

Look at the lower part of the https://laravel.com/docs/5.8/eloquent-relationships#one-to-one paragraph, it describes you need to enter foreign key as 2nd variable of the relationship definition, not the local key. 查看https://laravel.com/docs/5.8/eloquent-relationships#one-to-one段落的下部,它描述您需要输入外键作为关系定义的第二个变量,而不是本地键。

BTW: Your other relationship has the same problem: 顺便说一句:您的其他恋爱关系有相同的问题:

public function dataGroup()
{
    return $this->belongsTo('App\DataGroup', 'id');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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