Laravel Invalid argument supplied for foreach() problem - php

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?

Please Use this code
#foreach (json_decode($giveaway->items?:"{}") as $item)
I think the value of $giveaway->items is Null

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)

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?
is $this->_getInfoOfGame($game) return array ?
is 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);
Send the data as array to your views (way easier to debug), it's up to how you wrote your code, but i personally don't see the use of json_decode(... in the views.
Let us know how it goes.

Related

Laravel - htmlspecialchars() expects parameter 1 to be string, object given

I go this error:
htmlspecialchars() expects parameter 1 to be string, object given
I'm using in controller:
$data = '{"pr":{"code":"1"},"ac":[[{"icon":"web","action":"link","url":"asd"}]]}'
$newData = json_decode($data);
And i send it to the view as array: 'data' => $newData
And when i try to use $data into the view, it give me that error
Tried already to use $data->ac OR $data['ac'] but still the same...
Some help, please?
When you use a blade echo {{ $data }} it will automatically escape the output. It can only escape strings. In your data $data->ac is an array and $data is an object, neither of which can be echoed as is. You need to be more specific of how the data should be outputted. What exactly that looks like entirely depends on what you're trying to accomplish. For example to display the link you would need to do {{ $data->ac[0][0]['url'] }} (not sure why you have two nested arrays but I'm just following your data structure).
#foreach($data->ac['0'] as $link)
This is a link
#endforeach
if you real intention is to send the full array from the html to the controller, you can use the following code:
from the blade.php:
<input type="hidden" name="quotation" value="{{ json_encode($quotation,TRUE)}}">
in controller
public function Get(Request $req) {
$quotation = array('quotation' => json_decode($req->quotation));
//or
return view('quotation')->with('quotation',json_decode($req->quotation))
}
You could use serialize
<input type="hidden" name="quotation[]" value="{{serialize($quotation)}}">
But best way in this case use the json_encode method in your blade and json_decode in controller.
This is the proper way to access data in laravel :
#foreach($data-> ac as $link)
{{$link->url}}
#endforeach
Try use in your collection the json_encode().
https://www.php.net/manual/pt_BR/function.json-encode.php
public static function getTeamMemberInto($user_id){
$team = DB::table('members_team')
->join('teams', 'teams.id', '=', 'members_team.teams_id')
->join('employees', 'employees.id', '=', 'teams.leader_employees_id')
->where('members_team.employees_id', '=', $user_id)
->select('*')->first();
// dd($team);
if($team){
return json_encode($team, true);
}else{
return false;
}
}

its throwing error Invalid argument supplied for foreach()

Hello guys I'm new to laravel, so I got confuse. Somebody please clear my doubt, here is my view :
#foreach ($users as $user)
<span class="badge bg-important">{{ $user}}
</span>
#endforeach;
Here is my controller :
public function notification()
{
$users = DB::table('users')->where("Active", 0)->count();
return view('admin.layout.master',compact($users));
}
It's throwing error ;
Invalid argument supplied for foreach()
and if I remove foreach it's working fine can anybody clear my doubt why this occur
Change
compact($users)
to
compact('users')
The parameter is the name of the variable not the actual vaiable itself
First, as #meda stated, you need to fix your compact() statement.
The other issue is that your $users variable is being set to an integer, not the collection of users. Currently you are just counting the active users, not getting them.
Change
$users = DB::table('users')->where("Active", 0)->count();
To
$users = DB::table('users')->where("Active", 0)->get();

Echo Data From Collection to Blade View

I am simply (for my own testing purposes) trying to take data from a collection and pass it to a view. I am using Laravel.
I am getting my data from the GitHub API, converting it and putting it in a collection. From here it's passed to a view, but I can't output each individual field.
Here's some code:
$httpClient = new Client();
$response = $httpClient->get('https://api.github.com/users/<randomuser>');
$json = json_decode($response->getBody(), true);
$collection = collect($json);
return view('github')->with('github', $collection);
and my Blade file is
#foreach ($github as $git)
{{ $git }}
#endforeach
Now I thought it would be something as simple as {{ $git->email }} to output it, but I don't think the array keys are been sent (?)
Can anybody point me in the right direction of where I am going wrong?
Thanks in advance.
-Chris
Because it's a key => val array, you should loop it as such...
#foreach ($github as $key => $val)
Key: {{ $key }} ~~ Value: {{ $val}} <br />
#endforeach
However if you are looking to just grab the email, use the Collection's get method.
$email = $github->get('email')
Simple use get() method in your blade
$github->get('login')
Try this,
in your controller use View and in function
$data['github_data'] = $collection;
return view('github', $data);
and in blade
{{ dd($github_data) }}
When you are working with Laravel try to check if you have the correct structure before trying to manipulate it.
In this case, check that:
You are getting what you expect in $response after call $httpClient->get (if you are using Guzzle).
You have a $json with the structure that you expect to receive.
You have a Collection instance after calling collect().
You can use dd() or var_dump() to see what data structure you have. What´s probably happening is that you don´t have the structure that you think.

How to solve Object of class stdClass could not be converted to string while returning data into view in laravel?

This is my Laravel controller code
public function switchInfo($prisw, $secsw){
$cur_sw_pair_id = DB::table('sw_pairs')
->select('sw_pair_id')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->get();
$infolist = DB::table('cust_sw_pair')
->select('interface','pri_sw_vlan','sec_sw_vlan','pri_sw_admin_status','sec_sw_admin_status','description')
->where('sw_pair_id', '=', $cur_sw_pair_id)
->get();
return view('switchinfo.switchinfoview',compact('infolist'));
}
when this code executing it gives Object of class stdClass could not be converted to string error. How can i solve this? How can i pass my infolist into switchinfoview view without getting this error?
You can use pluck() to get value of a column.
Your first query should like this:
$cur_sw_pair_id = DB::table('sw_pairs')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->pluck('sw_pairs');
Keep the second query as it is.
Hope it will help.
The issue is that ->get() returns a collection, which is a 0-indexed collection of results (rows) from the database. In your case, you want to be using the following query:
$cur_sw_pair_id = DB::table('sw_pairs')
->select('sw_pair_id')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->first();
And then accessing the property sw_pair_id of $cur_sw_pair_id, like so:
$infolist = DB::table('cust_sw_pair')
->select('interface','pri_sw_vlan','sec_sw_vlan','pri_sw_admin_status','sec_sw_admin_status','description')
->where('sw_pair_id', '=', $cur_sw_pair_id->sw_pair_id)
->first();
Then, in your view, you can access all the properties (anything in the ->select() statement) of $infolist using $infolist->PROPERTY_HERE
Note that using ->first() is essentially using the same as ->get();, but you have to access the results of ->get() using an index, so
$cur_sw_pair_id[0]->sw_pair_id
Hope that provides some insight for you.

How to get php string values already set into an array

I get some values from laravel query builder. I would like to get and set these values into somewhere of my code. So i get undefined index exception message with the following codes :
laravel query :
$generatedRowValue = DB::table('myTableName')
->where('id', '=', 'someValue')
->where('anyOtherValue', '=', 'anotherValue')
->get();
I want to reach the values set in $generatedRowValue variable.
P.S. : Yeah i can get the values from the query, checked it out with var_dump(); dd(); etc. and i know my values are not undefined.
I'm new on this platform so i don't know how to reach these values.
Any help would be appreciated.
---UPDATE---
This is a simple HTML file which includes php partially.
<?php
$html = new simple_html_dom();
$html->load('<input type="text" value="'.$generatedRowValue['thevalueName'].'">')
?>
So i need $generatedRowValue['thevalueName'] 's value.
get() returns an array of objects. I think you want to use first() instead since you only expect one result.
$generatedRowValue = DB::table('myTableName')
->where('id', '=', 'someValue')
->where('anyOtherValue', '=', 'anotherValue')
->first();
By the way, you can also use the object syntax in your view (if you want)
$generatedRowValue->thevalueName

Categories