目前这是我的观点
{{ $leads }}
这是输出
{"error":false,"member":[{"id":"1","firstName":"first","lastName":"last","phoneNumber":"0987654321","email":"email@yahoo.com","owner":{
"id":"10","firstName":"first","lastName":"last"}}]}
我想要显示类似这样的内容
Member ID: 1
Firstname: First
Lastname: Last
Phone: 0987654321
Owner ID: 10
Firstname: First
Lastname: Last
答案 0 :(得分:23)
这很简单。 首先发送到视图解码变量(参见Laravel Views):
view('your-view')->with('leads', json_decode($leads, true));
然后只使用常见的刀片结构(参见Laravel Templating):
)@foreach($leads['member'] as $member)
Member ID: {{ $member['id'] }}
Firstname: {{ $member['firstName'] }}
Lastname: {{ $member['lastName'] }}
Phone: {{ $member['phoneNumber'] }}
Owner ID: {{ $member['owner']['id'] }}
Firstname: {{ $member['owner']['firstName'] }}
Lastname: {{ $member['owner']['lastName'] }}
@endforeach
答案 1 :(得分:4)
您可以使用json decode然后获得php数组,并将该值用作您自己的方式
<?php
$leads = json_decode($leads, true);
dd($leads);
答案 2 :(得分:4)
从laravel 5.5开始,您似乎可以使用@json($ leads)
答案 3 :(得分:3)
只需使用像这样的json_decode php函数将json数据转换为对象
$member = json_decode($json_string);
and pass to view in view
return view('page',compact('$member'))
在视图刀片
中Member ID: {{$member->member[0]->id}}
Firstname: {{$member->member[0]->firstname}}
Lastname: {{$member->member[0]->lastname}}
Phone: {{$member->member[0]->phone}}
Owner ID: {{$member->owner[0]->id}}
Firstname: {{$member->owner[0]->firstname}}
Lastname: {{$member->owner[0]->lastname}}
答案 4 :(得分:3)
对我来说,一切都是获取一个对象,对其进行编码,然后将该字符串传递给javascript script
标记。要做到这一点,你必须做一些替换。
首先使用双斜杠\
替换每个\\
,然后使用"
替换每个引用\"
。
$payload = json_encode($payload);
$payload = preg_replace("_\\\_", "\\\\\\", $payload);
$payload = preg_replace("/\"/", "\\\"", $payload);
return View::make('pages.javascript')
->with('payload', $payload)
然后在刀片模板中
@if(isset($payload))
<script>
window.__payload = JSON.parse("{!!$payload!!}");
</script>
@endif
这基本上允许你在php端获取一个对象,然后在javascript端有一个对象。
答案 5 :(得分:2)
在这种情况下,您可以这样
@foreach (json_decode($leads->member) as $member)
{{ $genre }}
@endforeach
答案 6 :(得分:1)
只需将 $ 移除到压缩方法中即可 返回视图(&#39;页面&#39;,压缩(&#39;会员&#39;))
答案 7 :(得分:0)
如果您的数据来自模型,您可以这样做:
应用\ HTTP \控制器\ SomeController 强>
public function index(MyModel $model)
{
return view('index', [
'data' => $model->all()->toJson(),
]);
}
<强> index.blade.php 强>
@push('footer-scripts')
<script>
(function(global){
var data = {!! $data !!};
console.log(data);
// [{..}]
})(window);
</script>
@endpush