我使用以下方法从刀片模板中提取数据:
@foreach ($links as $link)
<p>{{ ($link['title']) }} </p>
@endforeach
我想用:
@foreach ($links as $link)
<p>{{ ($link->title) }} </p>
@endforeach
但我不会将我的数据存储为控制器中的对象。 如何将数据存储为对象?
我的控制器代码:
foreach($json['items'] as $item) {
$link = [
'vidId' => $item['id']['videoId'],
'title' => $item['snippet']['title'],
'thumb' => $item['snippet']['thumbnails']['high']['url']
];
$links[] = $link;
}
$data['links'] = $links;
$data['v'] = $v;
return view('results', $data);
答案 0 :(得分:2)
将数组转换为object:
$obj_links = (object)$links;
答案 1 :(得分:1)
我重写了控制器:
foreach($json['items'] as $item) {
$link = new \stdClass;
$link -> vidId = $item['id']['videoId'];
$link -> title = $item['snippet']['title'];
$link -> thumb = $item['snippet']['thumbnails']['high']['url'];
$links[] = $link;
}
$data = [
'links' => $links,
'v' => $v
];
return view('results')->with($data);