Reputation: 17
I'm trying to get a list of things from the Steam service on the main page of the site. But I get an error: Invalid argument supplied for foreach()
.
My blade:
@foreach(json_decode($giveaway->items) as $item)
<img class="giveaway-item-img" src="https://steamcommunity-a.akamaihd.net/economy/image/class/570/{{$item->classid}}">
</div>
<div class="giveaway-item-name">{{$item->name}}</div>
@endforeach
And my Controller:
#GiveAway
$kolvo=\DB::table('giveaway_items')->where('status',0)->orderBy('id', 'desc')->count();
$giveaway = Giveaway::orderBy('id', 'desc')->first();
$giveaway_users = \DB::table('giveaway_users')
->where('giveaway_id', $giveaway->id)
->join('users', 'giveaway_users.user_id', '=', 'users.id')
->get();
$game = Game::orderBy('id', 'desc')->first();
$items = PagesController::sortByChance(json_decode(json_encode($this->_getInfoOfGame($game))));
But every time i got error. What could be the problem, how to fix the error?
Upvotes: 1
Views: 183
Reputation: 1222
I can see that you're using this in your php:
$items = PagesController::sortByChance(json_decode(json_encode($this->_getInfoOfGame($game))));
do you really need to use json_encode, then json_decode here?
$this->_getInfoOfGame($game)
return array ?PageController::sortByChange()
returning array ?I would suggest not to overuse json_encode, json_decode, keep them as arrays in your php codes, only json_decode Steam data (i assume you're taking data from steam);
Let us know how it goes.
Upvotes: 1
Reputation: 450
Please Use this code
@foreach (json_decode($giveaway->items?:"{}") as $item)
I think the value of $giveaway->items is Null
Upvotes: 3
Reputation: 33216
By default, json_decode
will return an object. You need to provide true
as the second argument to get an associative array.
@foreach(json_decode($giveaway->items, true) as $item)
Upvotes: 2