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;
}
}
Related
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.
I am working on a Laravel project in which I need to write a custom function, but when I call this function Laravel says:
Laravel : htmlentities() expects parameter 1 to be string, array given
Here is my function:
public static function get_checkup_time(){
$user_logged_in = Auth::user()->foreignkey_id;
$result = DB::table('doctors')
->select('checkuptime')
->where(['id'=>$user_logged_in])
->get();
return $result;
}
And this is my view in which I am trying to invoke this function .
#if(Auth::user()->userrolebit ==2)
{{
DateTimeFormat::get_checkup_time()
}}
#endif
i don't know what I am doing wrong here.
where() expects string as first parameter, so change this:
->where(['id'=>$user_logged_in])
to this:
->where('id', $user_logged_in)
If you are trying to return just checkup time you should do this in your method
public static function get_checkup_time()
{
$user_logged_in = Auth::user()->foreignkey_id;
$result = DB::table('doctors')
->select('checkuptime')
->where('id', $user_logged_in)
->first();
return $result->checkuptime;
}
Edit:
Following a downvote, I've seen that mine wouldn't work as well because the ->get() call should be replaced with ->first() as in #Paul's answer. See my comment below.
The $result you are returning from the method is not a string.
Therefore {{ DateTimeFormat::get_checkup_time() }} is the one returning the error.
Returning something like $result->checkuptime should do it.
I am trying to fetch data from a table by using a custom class .But it says htmlentities() expects parameter 1 to be string.
This is My DateTimeFormat Class .Here vitals is a table.which have vita_name attribute.
public static function get_vital_details($vital_id)
{
$result = DB::table('vitals')
->select('vita_name')
->where(['id' => $vital_id])
->get();
return $result;
}
This is the view i am trying to access the data .
<?php $vitalsinfo=DateTimeFormat::get_vital_details($vitaldetails->vital_id) ?>
#foreach($vitalsinfo as $vitalsinfo)
{{$vitalsinfo}}
#endforeach
i Am new to laravel .Any suggestions would be more than welcome. Thank You
You're trying to dipslay object as string, so try to use first() instead of get() to get just one object instead of collection:
$result = DB::table('vitals')
->select('vita_name')
->where(['id' => $vital_id])
->first();
And do just this (instead of #foreach construction) to display property of an object:
{{ $vitalsinfo->vita_name }}
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.
I have a piece of code and I'm trying to find out why one variation works and the other doesn't.
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'))->with('selections', $selections);
This allows me to generate a view of arrays for fixtures, teams and selections as expected.
However,
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'), compact('selections'));
does not allow the view to be generated properly. I can still echo out the arrays and I get the expected results but the view does not render once it arrives at the selections section.
It's oké, because I have it working with the ->with() syntax but just an odd one.
Thanks.
DS
The View::make function takes 3 arguments which according to the documentation are:
public View make(string $view, array $data = array(), array $mergeData = array())
In your case, the compact('selections') is a 4th argument. It doesn't pass to the view and laravel throws an exception.
On the other hand, you can use with() as many time as you like. Thus, this will work:
return View::make('gameworlds.mygame')
->with(compact('fixtures'))
->with(compact('teams'))
->with(compact('selections'));
I just wanted to hop in here and correct (suggest alternative) to the previous answer....
You can actually use compact in the same way, however a lot neater for example...
return View::make('gameworlds.mygame', compact(array('fixtures', 'teams', 'selections')));
Or if you are using PHP > 5.4
return View::make('gameworlds.mygame', compact(['fixtures', 'teams', 'selections']));
This is far neater, and still allows for readability when reviewing what the application does ;)
I was able to use
return View::make('myviewfolder.myview', compact('view1','view2','view3'));
I don't know if it's because I am using PHP 5.5 it works great :)
Laravel Framework 5.6.26
return more than one array then we use compact('array1', 'array2', 'array3', ...) to return view.
viewblade is the frontend (view) blade.
return view('viewblade', compact('view1','view2','view3','view4'));
Route::get('/', function () {
return view('greeting', ['name' => 'James']);
});
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
or
public function index($id)
{
$category = Category::find($id);
$topics = $category->getTopicPaginator();
$message = Message::find(1);
// here I would just use "->with([$category, $topics, $message])"
return View::make('category.index')->with(compact('category', 'topics', 'message'));
}
You can pass array of variables to the compact as an arguement
eg:
return view('yourView', compact(['var1','var2',....'varN']));
in view:
if var1 is an object
you can use it something like this
#foreach($var1 as $singleVar1)
{{$singleVar1->property}}
#endforeach
incase of scalar variable you can simply
{{$var2}}
i have done this several times without any issues
$data = [
'var1' => 'something',
'var2' => 'something',
'var3' => 'something',
];
return View::make('view', $data);