I have data in MySQL db, table 'user', column 'attributes' with json type as follow :
{"hp": {"remaining": 10, "total": 10}, "mp": {"remaining": 5, "total": 5}}
I want to update hp->remaining to 9 without updating the whole json but just the key
1st method :
$user = Users::find(1)->firstOrFail();
$user->attributes->hp->remaining = 9;
$user->save();
it failed saying Indirect modification of overloaded property App\Models\Users::$attributes has no effect
2nd method :
$user = Users::find(1)->firstOrFail();
$user->attributes = json_encode(array('hp' => array('remaining' => 9)));
$user->save();
it didn't have any errors but it failed because it updated the whole json attributes and lost 'hp->total' key and all mp, thus became
{"hp": {"remaining": 9}}
I know I can just replace the whole json with updated key like:
$user = Users::find(1)->firstOrFail();
$user->attributes = json_encode(array(
'hp' => array('remaining' => 9, 'total' => 10),
'mp' => array('remaining' => 5, 'total' => 5)
));
$user->save();
However is there a way to update just the key?