Javascript 使用laravel/php循环json
我运行函数Javascript 使用laravel/php循环json,javascript,php,json,laravel,loops,Javascript,Php,Json,Laravel,Loops,我运行函数DB::table('table_name')->get()和json_encode($posts)将结果作为{{posts}}输出: [{"id":1,"author":"Laravel_Newbie","author_id":2,"posted_date":"2018-05-12 00:00:00","body":"Hello to Laravel!"},{"id":2,"author":"Other User","author_id":3,"posted_date":"2018-
DB::table('table_name')->get()
和json_encode($posts)
将结果作为{{posts}}
输出:
[{"id":1,"author":"Laravel_Newbie","author_id":2,"posted_date":"2018-05-12 00:00:00","body":"Hello to Laravel!"},{"id":2,"author":"Other User","author_id":3,"posted_date":"2018-05-12 00:00:00","body":"Hello and an another body ."}]
现在,我需要以更好的方式显示信息。那么,我喜欢什么`
<div>
<b> author </b>
<b> posted_date </b>
<p> body </p>
</div>
作者
过帐日期
身体
`等等,使用数据库中的数据。那么,我该怎么做呢?不要使用
json\u encode($posts)
并尝试它(刀片代码)
@foreach($posts as$item)
{{$item->author}
{{$item->posted_date}
{{$item->body}
@endforeach
在php中,您可以使用$posts对象循环结果并显示它们:
foreach($posts as $post){
?>
<div>
<b><?php echo $post->author; ?></b>
<b><?php echo $post->posted_date; ?></b>
<p><?php echo $post->body; ?></p>
</div>
<?php
}
foreach($posts as$post){
?>
Json_将您的Json字符串解码为数组,然后您可以在其中循环。谢谢,但是。author不工作,但是->author工作了!
foreach($posts as $post){
?>
<div>
<b><?php echo $post->author; ?></b>
<b><?php echo $post->posted_date; ?></b>
<p><?php echo $post->body; ?></p>
</div>
<?php
}