So I get this error Undefined variable: games even if my variable is defined in controller:
My Controller:
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;
}
The problem seems to be from the getGames() function
my blade:
@for ($i = 0; $i < 10; $i++)
<div class="item" style="background-image:url({{ $games->image }})">
</div>
@endfor
and if needed here is my Model 'Game' which is used in that getGames() function
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;
}
}