I want to create a ticketing system with Laravel. I store the chat data's in json data type. when user want to send her/his ticket i get her/his data and store its with these code:
$messages = array([
'message_title' => post('message_title'),
'message_body' => post('message_body'),
'type' => 'q',
'created_at' => date('Y/m/d H:i:s'),
]);
$messagesToJson = json_encode($messages);
$query = DB::table('test')->insert([
'level_id' => post('message_level'),
'status' => 0,
'ticket_id' => post('user_id') . time(),
'user_id' => post('user_id'),
'backend_user_id' => '',
'messages' => $messagesToJson,
]);
these data store in MySQL:
[
{"message_title":"This is title","message_body":"Some text ... .","type":"q","created_at":"2020\/02\/29 20:42:26"}
]
But my problem is: when the user want ask a new question in this ticket how can i insert a new object in this array like this:
[
{"message_title":"This is title","message_body":"Some text ... .","type":"q","created_at":"2020\/02\/29 20:42:26"},
{"message_title":"Question 2","message_body":"Some text in question 2 ... .","type":"q","created_at":"2020\/02\/29 21:42:26"}
]
My update code is:
$messages = array(
'message_body' => post('sendText'),
'type' => 'q',
'created_at' => date('Y/m/d H:i:s'),
);
$messagesToJson = json_encode($messages);
$updateQuery = DB::update("UPDATE synon_subsystem_ticketing
SET messages = JSON_SET(messages, '$[$countMessages]', '$messagesToJson') WHERE id = '$id'");