From 65766efd7136964d6c7a4fe6ddb973a534417345 Mon Sep 17 00:00:00 2001 From: Julian Prieber Date: Thu, 16 Feb 2023 14:22:18 +0100 Subject: [PATCH] Added model to save userdata UserData::saveData(ID, 'foo', 'bar'); UserData::getData(ID, 'foo'); UserData::removeData(ID, 'foo'); --- app/Models/UserData.php | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/Models/UserData.php diff --git a/app/Models/UserData.php b/app/Models/UserData.php new file mode 100644 index 0000000..391849e --- /dev/null +++ b/app/Models/UserData.php @@ -0,0 +1,56 @@ +first(); + + if (!$userData) { + return; + } + + $data = json_decode($userData->image, true); + $data[$key] = $value; + + $userData->image = json_encode($data); + $userData->save(); + } + + public static function getData($userId, $key) + { + $userData = self::where('id', $userId)->first(); + + if (!$userData || !$userData->image) { + return null; + } + + $data = json_decode($userData->image, true); + + return isset($data[$key]) ? $data[$key] : null; + } + + public static function removeData($userId, $key) + { + $userData = self::where('id', $userId)->first(); + + if (!$userData || !$userData->image) { + return; + } + + $data = json_decode($userData->image, true); + + if (isset($data[$key])) { + unset($data[$key]); + $userData->image = json_encode($data); + $userData->save(); + } + } +}