无法解码来自数据库的数据(Php LAravel 5.3)
Unable to decode data from db (Php LAravel 5.3)
我正在开发 laravel 应用程序,我将我的数据以 JSON 编码形式保存为
{"name":"Ali","email":"testdc@gamil.com"}
它在数据库文本字段中显示如上
在我的方法中,我获取的数据为
function users($id, Request $request)
{
$method = $request->method();
if($request->isMethod('GET')) {
$users = DB::table('user_settings')->select('notification_setting')->first();
print_r( $notification_smtp);
die;
return view('setting/user');
}
}
下面是上面代码的输出:
stdClass Object ( [notification_setting] => {"name":"Ali","email":"testdc@gamil.com"} )
如果我尝试对其进行解码,它会给出一个错误,因为 json_decode 第二个参数应该是传递的字符串对象
我怎样才能得到这种格式的响应?
stdClass Object ( [name] => Ali [email] => test@gmail.com )
我怎样才能做到这一点?
试试,print_r( json_decode($notification_smtp->notification_setting) );
你尝试连载了吗?比如获取一个带有解析对象的 tmp 变量
//your code
$users = DB::table('user_settings')->select('notification_setting')->first();
print_r( $notification_smtp);
die;
//try this
$tmp = (string) $user;
dd($tmp);
如果没有帮助,请阅读 laravel 文档,它真的很好。
如果还是没有帮助到你,google 序列化和反序列化 ;)
我正在开发 laravel 应用程序,我将我的数据以 JSON 编码形式保存为
{"name":"Ali","email":"testdc@gamil.com"}
它在数据库文本字段中显示如上
在我的方法中,我获取的数据为
function users($id, Request $request)
{
$method = $request->method();
if($request->isMethod('GET')) {
$users = DB::table('user_settings')->select('notification_setting')->first();
print_r( $notification_smtp);
die;
return view('setting/user');
}
}
下面是上面代码的输出:
stdClass Object ( [notification_setting] => {"name":"Ali","email":"testdc@gamil.com"} )
如果我尝试对其进行解码,它会给出一个错误,因为 json_decode 第二个参数应该是传递的字符串对象
我怎样才能得到这种格式的响应?
stdClass Object ( [name] => Ali [email] => test@gmail.com )
我怎样才能做到这一点?
试试,print_r( json_decode($notification_smtp->notification_setting) );
你尝试连载了吗?比如获取一个带有解析对象的 tmp 变量
//your code
$users = DB::table('user_settings')->select('notification_setting')->first();
print_r( $notification_smtp);
die;
//try this
$tmp = (string) $user;
dd($tmp);
如果没有帮助,请阅读 laravel 文档,它真的很好。
如果还是没有帮助到你,google 序列化和反序列化 ;)