eloquent-json-relations: Relationship queries fail for string Ids

Hi. The package works wonderfully and has been nothing short of incredible since discovering it. I noticed though, that relationship queries fail when used with records that have ids of random strings (as is the case in some parts of my implementation) instead of the expected integer.

As an example, a relation defined like so:

public function collectedScrap()  
{
    return $this->belongsToJson(CollectedScrap::class, 'collection_ids->collection_id');
}

where the collection_ids is an array of ids that are of six random letters and numbers:

["IJSM9","JSN3HG","HYSH7T"]

returns an empty array. I would like to know if the package even supports this behaviour and how I might go about implementing a workaround if it doesn’t.

Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 28 (13 by maintainers)

Most upvoted comments

Oh, that was the problem. It works fine now. Thanks for the assistance.

In your data, there is no location_ids key, but coverage_zone_coordinates itself contains the related IDs.

I only see location_ids as a completely separate attribute.

With this data structure, the relationship needs to be:

public function coverage_zone_coordinates()
{
   return $this->belongsToJson(MajorArea::class, 'coverage_zone_coordinates');
}