2023-02-16 14:22:18 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class UserData extends Model
|
|
|
|
{
|
|
|
|
protected $table = 'users';
|
|
|
|
protected $fillable = ['image'];
|
|
|
|
|
|
|
|
public static function saveData($userId, $key, $value)
|
|
|
|
{
|
|
|
|
$userData = self::where('id', $userId)->first();
|
|
|
|
|
|
|
|
if (!$userData) {
|
2023-02-16 16:10:41 +01:00
|
|
|
return "null";
|
2023-02-16 14:22:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 16:10:41 +01:00
|
|
|
$data = json_decode($userData->image, true) ?? [];
|
2023-02-16 14:22:18 +01:00
|
|
|
$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) {
|
2023-02-16 16:10:41 +01:00
|
|
|
return "null";
|
2023-02-16 14:22:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 16:10:41 +01:00
|
|
|
$data = json_decode($userData->image, true) ?? [];
|
2023-02-16 14:22:18 +01:00
|
|
|
|
|
|
|
return isset($data[$key]) ? $data[$key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function removeData($userId, $key)
|
|
|
|
{
|
|
|
|
$userData = self::where('id', $userId)->first();
|
|
|
|
|
|
|
|
if (!$userData || !$userData->image) {
|
2023-02-16 16:10:41 +01:00
|
|
|
return "null";
|
2023-02-16 14:22:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 16:10:41 +01:00
|
|
|
$data = json_decode($userData->image, true) ?? [];
|
2023-02-16 14:22:18 +01:00
|
|
|
|
|
|
|
if (isset($data[$key])) {
|
|
|
|
unset($data[$key]);
|
|
|
|
$userData->image = json_encode($data);
|
|
|
|
$userData->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|