Appending Key->Value with an existing JSON Data

Posted 8 months ago by abudo

On a table column, i can save a json data in the format

{"at_quest": ["1.1"], "quest_1.1_score": "67", "quest_1.1_passed": "2018-03-20 09:48:47.886849"}

I need to update this data in the same format only some characters needs to change on subsequent updates i.e

 {"at_quest": ["1.1"], "quest_1.1_score": "67", "quest_1.1_passed" : "2018-03-20 09:48:47.886849","at_quest": ["1.2"], "quest_1.2_score": "67", "quest_1.2_passed" : "2018-03-20 09:48:47.886849"}

My code

$obj = DB::table('table')->where('user_id',$userIid)->where('product_id',$productId)->value('history');

        $at = Carbon::now();
        $data["quest_".$questId."_score"] = $percentageScore;
        $data["quest_".$questId."_passed"] =  $at;
        $json_data = json_encode($data);
        UserTrainingHistory::where('user_id', $userIid)
         ->where('product_id', $productId)
         ->update(['history' => $json_data]);
         dd('we will update the quest');
        $at = Carbon::now();
        $data["quest_".$questId."_score"] = $percentageScore;
        $data["quest_".$questId."_passed"] =  $at;
        $data1 = json_encode($data);

        $UserTraininghHstory = new UserTrainingHistory;
        $UserTraininghHstory->user_id = $userIid;
        $UserTraininghHstory->product_id = $productId;
        $UserTraininghHstory->history = $data1;


what i got stored in database is this, which is wrong

{"0": {"0": {"at_quest": "1.1", "quest_1.1_score": "100", "quest_1.1_passed": {"date": "2018-03-21 07:16:30.099540", "timezone": "UTC", "timezone_type": 3}}, "at_quest": "1.2", "quest_1.2_score": "67", "quest_1.2_passed": {"date": "2018-03-21 07:17:38.727736", "timezone": "UTC", "timezone_type": 3}}, "at_quest": "1.3", "quest_1.3_score": "100", "quest_1.3_passed": {"date": "2018-03-21 09:58:01.707335", "timezone": "UTC", "timezone_type": 3}}

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.