【问题标题】:Unable to decode data from db (Php LAravel 5.3)无法解码来自 db 的数据(Php Laravel 5.3)
【发布时间】:2016-12-30 10:43:03
【问题描述】:

我正在开发 laravel 应用程序,并将我的数据以 JSON 编码形式保存为

{"name":"Ali","email":"testdc@gamil.com"} 

它在db文本字段中显示如上

在我的方法中,我得到的数据是

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 )

我怎样才能做到这一点?

【问题讨论】:

  • 能否贴出用于解码 JSON 的语句?
  • 它说 json_decode() 期望参数 1 是字符串,给定对象
  • 是的,我们需要的是您在代码中用于解码 JSON 的方法/语句。

标签: php json laravel-5.3


【解决方案1】:

试试看,print_r( json_decode($notification_smtp->notification_setting) );

【讨论】:

    【解决方案2】:

    你试过序列化吗?比如用解析的对象获取一个 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 文档,它真的很好。

    Laravel Serialization

    如果仍然没有帮助你,谷歌序列化和反序列化;)

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 2017-03-16
      • 2017-09-28
      • 2020-10-21
      • 2017-03-03
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多