0

When I run the code I get no error but the data I am trying to display is not displaying it's just blank.. can someone tell me what I'm doing wrong?

My controller:

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

The 'Game' Model that is used in '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', 1)->first();

       if(empty($game)) return array();

       return $game;
   }
}

The view:

@foreach ($games as $game)

    <div class="gold">$ {{ $game['price'] }}</div>

@endforeach
5
  • first() returns only an object Commented Aug 7, 2019 at 20:05
  • 1
    Does $this->data->items contain anything? Does $item contain anything? Commented Aug 7, 2019 at 20:07
  • Yes it contains this 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); } Commented Aug 7, 2019 at 20:08
  • 1
    Did you try to dd($this->data->items) or dd($item)? Commented Aug 7, 2019 at 20:10
  • I believe that $game = DB::table('products')->where('id', 1)->first(); Is always looking for the game with id 1. Commented Aug 7, 2019 at 20:16

1 Answer 1

2

I think you are over-complicating things. You could simplify your flow like this:

Given your provided code, it seems like you are using a custom table name ('products') in your Game model. So we'll address this first:

Game.php

class Game extends Model
{
    protected $table = 'products'; //
}

Now, it seems like you're searching an array of Game ids ($this->data->items). If so, you could make use of Eloquent for your query, specially the whereIn() method:

YourController.php

public function openingPage($id)
{
    $games = Game::whereIn('id', $this->data->items)->get();

    return view('caseopener')->with('games', $games);
}

Optionally, if you want to make sure of just returning the id, name, price and image of each Game/product, you could format the response with API Resources:

php artisan make:resource GameResource

Then in your newly created class:

app/Http/Resources/GameResource.php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class GameResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'price' => $this->price,
            'image' => $this->image,
        ];
    }
}

So now just update your controller:

YourController.php

use App\Http\Resources\GameResource;

public function openingPage($id)
{
    $games = Game::whereIn('id', $this->data->items)->get();

    return view('caseopener')->with('games', GameResource::collection($games));
} //                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sign up to request clarification or add additional context in comments.

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.