spatie / laravel-schemaless-attributes

Add schemaless attributes to Eloquent models
https://spatie.be/en/opensource
MIT License
996 stars 58 forks source link

Support for nested properties? #70

Closed nathan-io closed 4 years ago

nathan-io commented 4 years ago

This question/issue is on v1.8.0, Laravel 7.26.1. Does the package support nested properties - $model->extra_attribute->someProperty->someNestedProperty? If so, how do I set one?

Here's what I tried on our User model, where one of our schemaless attributes is timezone.

>>> $user
=> App\User {#4458
     id: 1,
     name: "{"first":"John","last":"Doe","formatted":"John Doe"}",
     email: "mail@mail.com",
     phone: null,
     locale: "en",
     timezone: null,
     address: null,
     birthday: null,
     avatar: null,
     gender: null,
     privacy: null,
     email_verified_at: null,
     phone_verified_at: null,
     banned_at: null,
     deleted_at: null,
     created_at: "2020-09-13 03:00:00",
     updated_at: "2020-09-13 03:01:35",
   }

>>> $user->timezone->someKey = 'someVal';
=> "someVal"
>>> $user->timezone->someKey
=> "someVal"

This works as expected. Yet if I try to create a property within someVal:

>>> $user->timezone->someKey->someNestedKey = 'someNestedVal';
PHP Notice:  Indirect modification of overloaded property Spatie/SchemalessAttributes/SchemalessAttributes::$someKey has no effect in Psy Shell code on line 1
PHP Warning:  Attempt to assign property 'someNestedKey' of non-object in Psy Shell code on line 1
=> null

Thank you.

freekmurze commented 4 years ago

This is currently not supported. If you need this and the solution is simple, I'd accept a PR with tests that adds this.