Relationships laravel doesn’t work after 5th position

huangapple go评论71阅读模式
英文:

Relationships laravel doesn't work after 5th position

问题

我有2个模型:

模型1 -> 有多个模型2

public function model2(){
    return $this->hasMany(Model2::class, 'field_fk_id');
}

模型2 -> 属于模型1

public function model1(){
    return $this->belongsTo(Model1::class,'id');
}

当我尝试获取模型2的前5个元素的关系时,一切正常。但是,当我尝试获取第6个或更高的ID时,返回null

示例:正确引用

"status": true,
    "message": "Model 2",
    "data": {
        "id": 4,
        "name": "Name example",
        "field_fk_id": 1,
        "created_at": "2023-04-04T09:23:41.000000Z",
        "updated_at": "2023-04-04T09:23:41.000000Z",
        "model1": {
            "id": 2,
            "name": "Name parent example",
            "created_at": "2023-04-04T09:23:41.000000Z",
            "updated_at": "2023-04-04T09:23:41.000000Z"
        }
    }

错误引用:

"status": true,
        "message": "Model 2",
        "data": {
            "id": 11,
            "name": "Name example 11",
            "field_fk_id": 3,
            "created_at": "2023-04-04T09:23:41.000000Z",
            "updated_at": "2023-04-04T09:23:41.000000Z",
            "model1": null
        }

当我尝试从模型1获取关系时,hasMany关系正常工作。返回所有引用。

有没有什么办法来修复这个问题?

附注:'field_fk_id'的引用存在,我在请求时检查过,也在数据库中检查过。

英文:

I have 2 models:

model 1 -> hasMany(model2)

public function model2(){
    return $this->hasMany(Model2::class, 'field_fk_id');
}

model 2 -> belongsTo(model1)

public function model1(){
        return $this->belongsTo(Model1::class,'id');
    }

When i try to get the relationship of 5 first elements of model 2, work well. When i try to get the 6th or highest id, return null.

Example: Ok reference

"status": true,
    "message": "Model 2",
    "data": {
        "id": 4,
        "name": "Name example",
        "field_fk_id": 1,
        "created_at": "2023-04-04T09:23:41.000000Z",
        "updated_at": "2023-04-04T09:23:41.000000Z",
        "model1": {
            "id": 2,
            "name": "Name parent example",
            "created_at": "2023-04-04T09:23:41.000000Z",
            "updated_at": "2023-04-04T09:23:41.000000Z"
        }
    }

Wrong reference:

"status": true,
        "message": "Model 2",
        "data": {
            "id": 11,
            "name": "Name example 11",
            "field_fk_id": 3,
            "created_at": "2023-04-04T09:23:41.000000Z",
            "updated_at": "2023-04-04T09:23:41.000000Z",
            "model1": null
        }

When i try to get the relationship since model 1, the relation hasMany work well. Return all the referencies.

Any idea for fix this?

P.S.: the references of field_fk_id exists, i check it at the request and i check it at the database.

答案1

得分: 2

The second parameter of belongsTo is a foreign key. So it should be:

public function model1(){
   return $this->belongsTo(Model1::class,'field_fk_id');
}
英文:

The second parameter of belongsTo is a foreign key. So it should be:

public function model1(){
   return $this->belongsTo(Model1::class,'field_fk_id');
}

huangapple
  • 本文由 发表于 2023年4月4日 18:03:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定