从json数据中获取特定值

时间:2018-05-26 08:37:49

标签: php json laravel

我的PagesController function看起来像这样:

public function home(){
        if (\Session::has('user_id')){
            $posts = DB::table('posts')->get();
            $user = DB::table('users')->where('id', \Session::get('user_id'))->first();
            return view('home', compact('posts', 'user'));
        }else{
            return view('welcome');
        }
    }

而且,在我看来home.blade.php,当我使用{{$user->user_json}}打印出数据时,我得到:

{"id":6,"friends":[],"posts":[],"messages":[],"notifications":[],"pin":""}

现在,我如何获得类似用户ID的特定值?

2 个答案:

答案 0 :(得分:1)

如果您发送了json,则必须在刀片中进行解码,并且当我们在第二个参数中传递true时它将返回一个关联数组

$ user_data = json_decode($ user_json,true);

答案 1 :(得分:0)

$json = json_decode($user->user_json, true);
{{$json['id']}} 

$obj= json_decode($user->user_json);
$obj->id;

尝试以上代码即可。

相关问题