Arrays 在Laravel中显示JSON变量值
我在最近的Laravel 4.1项目中收到了这个消息,我相信这是一个JSON回报Arrays 在Laravel中显示JSON变量值,arrays,json,laravel,Arrays,Json,Laravel,我在最近的Laravel 4.1项目中收到了这个消息,我相信这是一个JSON回报 {"id":5,"owner":"2","message":"What a wonderful shoot today in Belgium!","post_at":"2","created_at":"2014-02-25 15:05:50","updated_at":"2014-02-25 19:05:50"} 但是我通常可以这样显示它: echo $post->id; 但不知何故,它没有返回“5”,而
{"id":5,"owner":"2","message":"What a wonderful shoot today in Belgium!","post_at":"2","created_at":"2014-02-25 15:05:50","updated_at":"2014-02-25 19:05:50"}
但是我通常可以这样显示它:
echo $post->id;
但不知何故,它没有返回“5”,而是什么也没有返回。
但是当我使用$post->toArray()转换为数组时代码>并尝试使用echo$post['id']查看代码>它成功返回“5”
使用echo$post->id返回其值的最佳方式是什么代码>?我还尝试使用$post->toJSON()确保变量为JSON代码>但仍返回空。检查此项
$post=post::where('id','=',5)->get()(不要忘记get())
或
$post=post::where('id',5)->get()(不要忘记get())
或
$post=post::find(5);
回答可能是:
Response::json($post);
答复:json($员额);如果您想像对象一样访问它,为什么要将其作为JSON返回?也就是说,既然它是JSON,JSON\u decode
,就是您要查找的内容:。这将把JSON转换为stdClass object.Awesome。它工作得很好!我从一个服装模型类中获得了JSON,该类带有querystatic::where('owner',$meandfriends)->orderby('updated_at','desc')->get()
您的查询已经过测试,并且运行正常,但这不是我的意思,不过感谢我现在意识到了这一点,我应该这样返回:如果(!is_null('stream=static::where('owner',$meandfriends)->orderby('updated_at','desc')->get()){return json_decode($stream)}
而不是:如果(!is_null($stream=static::where('owner',$meandfriends)->orderby('updated_at','desc')->get()){return$stream;}
他想在模板中正确地执行,而不是用jQuery。
$post = Post::where('id',5)->get()
$post = Post::find(5);
Response::json($post);
$.get('url', {'id': id}, function (obj) {
var te=obj.id;
alert(te);
});