第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

未定義的變量即使定義了 Laravel

未定義的變量即使定義了 Laravel

PHP
牛魔王的故事 2021-11-13 19:01:44
所以我得到這個(gè)錯(cuò)誤 Undefined variable: games 即使我的變量是在控制器中定義的:我的控制器: public function openingPage($id) {      $this->getCaseData($id);      $this->getGames();      return view('caseopener', ['cases' => $this->data])->with('games',$games);;    }    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);        }        $this->data = $data;    }   private function getGames() {      $games = array();      foreach ($this->data->items as $item) {          $game = new Game($item);          $games[] = array(            'id' => $game->id,            'name' => $game->name,            'image' => $game->image,          );      }      return $games;  }問(wèn)題似乎來(lái)自 getGames() 函數(shù)我的刀片:@for ($i = 0; $i < 10; $i++)                      <div class="item" style="background-image:url({{ $games->image }})">            </div>          @endfor如果需要,這里是我的模型“游戲”,用于該 getGames() 函數(shù)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', $this->id)->first();       if(empty($game)) return array();       return $game;   }}
查看完整描述

2 回答

?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

你忘了分配$gamesin openingPage(),試試這個(gè):


public function openingPage($id) {


     $this->getCaseData($id);


     $games = $this->getGames();


     return view('caseopener', ['cases' => $this->data])->with('games',$games);;

}


查看完整回答
反對(duì) 回復(fù) 2021-11-13
?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊

您忘記將游戲分配給$games變量。


此外,輸出值時(shí)有一個(gè)錯(cuò)誤:$games->image應(yīng)該是$games[$i]['image']。(訪問(wèn)特定游戲并獲取image值作為數(shù)組值)


如果將來(lái)有可能會(huì)有多于或少于 10 場(chǎng)比賽,我會(huì)將for循環(huán)替換為foreach:


@foreach($games in $game)

    <div class="item" style="background-image:url({{ $game['image'] }})">

    </div>

@endforeach


查看完整回答
反對(duì) 回復(fù) 2021-11-13
  • 2 回答
  • 0 關(guān)注
  • 107 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)