这是我第一次使用Mysql json列类型 当我通过 Laravel api 发出发布请求来创建新用户时 像这样
"username:andy21, password:123456, profile:{"home_town":"germany"}"
我将配置文件保存在 Json 列中 当我检索它时,它在 json 对象上的每个“之前都有反斜杠 像这样
"profile":"{\"home_town\":\"germany\"}"
我在 Laravel Controller 中的返回看起来像这样
return response()->json(['profile'=>$user]);
为什么它有这些反斜杠以及如何删除它?
最佳答案
如果您的 $user
变量采用 JSON 格式,请尝试此操作:
return response()->json(['profile' => json_decode($user)]);
关于Mysql json列输出反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171866/