archtechx / virtualcolumn

Eloquent Virtual Column package.
MIT License
70 stars 12 forks source link

[Question] how to deal with nested data? #13

Closed abanghendri closed 1 year ago

abanghendri commented 1 year ago

Hello, I came here because I am working with ypur multitenancy package, it is really awesome package But, I need help to deal with nesting data e.g:

"ccompany": " Tenant Company name",
"address": {
" street":"company street",
"state": " address state"
},
"social": {
" instagram": "https://instagram.com"
}

Or even deeper data , I cannot use $tenant->update(['social->instagram' => 'newlink'])

So how to deal with it?

timyourivh commented 1 year ago

The bestway to deal with this:

// Get the latest post.
$post = Post::latest()->first();

// Get the meta attributes as an array
$meta = $post->meta;

// Update the custom_value.
$meta['custom_value'] = 'somevalue';

// Update the meta attributes in the model.
$post->meta = $meta;

// Save the changes.
$post->save();

The record should now be updated:

{
    "body": "My very first test post!",
    "meta": {
        "custom_key": "somevalue"
    },
    "title": "Test Post",
    "created_at": "2023-08-16 10:00:59",
    "updated_at": "2023-08-16 10:00:59"
}