eloquent-json-relations: Many-To-Many Relationship doesnt seem to work with uuid

Hey there, just tried to use a many to many relationship where both models have a uuid and i cant seem to get it working!

class Auctionhouse extends Model {
	use HasJsonRelationships;

	protected $casts = [
		'itemCollection' => 'json',
	];

	protected $attributes = [
		'itemCollection' => '[]'
	];

	public function soldItems(): BelongsToJson {
		return $this->belongsToJson(Item::class, 'itemCollection[]->id');
	}
}
class Item extends Model {
	use HasJsonRelationships;

	public function soldInAuctionhouse(): HasManyJson {
		return $this->hasManyJson(Auctionhouse::class, 'itemCollection[]->id');
	}
}

Item Id is a uuid via a HasUuid Trait that sets incrementing to false, keyType to ‘string’ and primaryKey to ‘id’;

Idea is to add items to the auctionhouse via sync ->

$ac->soldItems()->sync(['077df06d-1c38-4fd6-b141-3544c6ddbc27' => ['price' => '200g']])->save());

Migrations are just using $table->json('itemCollection'); on the Auctionhouse Migration.

Database data looks like this

[{"id":"077df06d-1c38-4fd6-b141-3544c6ddbc27","price":"200g"}]

If i remove the json cast i cant seem to load the soldItems via $ac->soldItems as this either returns an empty Eloquent Collection

Illuminate\Database\Eloquent\Collection {#560 ▼
  #items: []
  #escapeWhenCastingToString: false
}

if the cast for the itemCollection is json/array it will throw this error array_key_exists(): Argument #2 ($array) must be of type array, null given which is similiar to issue

Hopefully i provided all necessary informations! Let me know if something is missing!

Greetings

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 33 (15 by maintainers)

Most upvoted comments

Will recreate a repo and post its link here!