【发布时间】:2019-12-15 12:13:47
【问题描述】:
当我运行代码时,我没有收到任何错误,但我试图显示的数据没有显示,它只是空白。有人可以告诉我我做错了什么吗?
我的控制器:
public function openingPage($id) {
$this->getGames();
$games = $this->getGames();
return view('caseopener')->with('games',$games);
}
private function getGames() {
$games = array();
foreach ($this->data->items as $item) {
$game = new Game($item);
$games[] = array(
'id' => $game['id'],
'name' => $game['name'],
'price' => $game['price'],
'image' => $game['image'],
);
}
return $games;
}
“getGames 函数”中使用的“Game”模型:
class Game extends Model
{
private $id;
public $data;
public function __construct($id) {
parent::__construct();
$this->id = $id;
$this->data = $this->getData();
}
private function getData() {
$game = DB::table('products')->where('id', 1)->first();
if(empty($game)) return array();
return $game;
}
}
观点:
@foreach ($games as $game)
<div class="gold">$ {{ $game['price'] }}</div>
@endforeach
【问题讨论】:
-
first() 只返回一个对象
-
$this->data->items是否包含任何内容?$item是否包含任何内容? -
是的,它包含这个
private function getCaseData($id) { $items = DB::table('cases')->where('id', $id)->get(); $data = @$items[0] ? $items[0] : array(); if(isset($data->items)) { $data->items = json_decode($data->items, true); } -
你试过
dd($this->data->items)或dd($item)吗? -
我相信
$game = DB::table('products')->where('id', 1)->first();一直在寻找id为1的游戏。
标签: php arrays database laravel laravel-blade