eloquent-json-relations: Strange bug when calling relationship in accessor
Strange but was trying to call a relationship in an accessor like so
public function getOptionsAttribute($value) {
//wanted to loop through the actual relation because it carries the additional data needed
// just a test...to see what is returned
error_log(print_r( $this->roles->get()->toArray(), true ));
return json_decode($value, true);
}
then if I call the relationship or try to retrieve the options attribute like so… (I used tinker to test)
\App\User::find(7)->roles
or
\App\User::find(7)->options
…I get the following
PHP Notice: Undefined property: App/User::$roles[]->role_id in /path_to_project/vendor/staudenmeir/eloquent-json-relations/src/Relations/BelongsToJson.php on line 97
line 97 refers to this line below in the addConstraints() method of BelongsToJson.php
$this->query->whereIn($table.'.'.$this->ownerKey, (array) $this->child->{$this->foreignKey});
Calling other relationships this way, within the accessor, seem to work except BelongsToJson
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 22 (10 by maintainers)
Check out the latest release, it should fix your issue.
This code works for me. When I remove
$casts, I get the “PHP Warning: array_key_exists() expects parameter 2 to be array” error you mentioned earlier.