1

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;
   }
}
2

2 Answers 2

2

You forgot to assign $games in openingPage(), try this:

public function openingPage($id) {

     $this->getCaseData($id);

     $games = $this->getGames();

     return view('caseopener', ['cases' => $this->data])->with('games',$games);;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I updated it and now I get Trying to get property of non-object. This error is from the view blade now
Because $games is an array of arrays, not an array of objects.
I answered your original question, please accept and upvote if the original error is gone, if you have another problem open a new question.
how can I fix that? can you help me please?
Sorry I would like to be helpful but this is not the way it works here at SO, we cannot debug all your code, if you want you can edit your question and add details
1

As @dparoli mentioned, you forgot to assign the games to the $games variable.

In addition there's a mistake when outputting the value: $games->image should be $games[$i]['image']. (Access a specific game and get the image value as an array value)

If there is any chance there might be more or less than 10 games in the future I would replace the for loop with foreach:

@foreach($games in $game)
    <div class="item" style="background-image:url({{ $game['image'] }})">
    </div>
@endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.