php - 从 json 数据中获取特定值
问题描述
我有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?
解决方案
如果你发送了 json,你必须在刀片中解码,它会返回一个关联数组,因为我们在第二个参数中传递了 true
$user_data = json_decode($user_json, true);
推荐阅读
- python - 如何使用 K-nearest neighbour 训练和测试数据?
- google-apis-explorer - 无法从 Google 课堂学生资料中接收电子邮件
- javascript - 明确使用“任何”类型时,打字稿编译器有没有办法抛出错误?
- javascript - 如何使用可折叠项目定位列表中的每个项目?
- kubernetes - 如何使 kubernetes pod 在没有密钥的情况下无法解密 kubernetes 机密?
- python - 如何在没有 sys.path.append 的 Python 中进行相对导入,既没有 -m falg 也没有 __init__.py 文件?
- python-3.x - 使用 apply lambda 检查条件时,Series 的真值不明确
- gensim - Gensim 和 Mallet 的整体一致性
- php - 通过 slug 或标题 wordpress 获取页面内容
- dfd - DFD 是真实的还是只是学术的?