got this error "htmlspecialchars() expects parameter 1 to be string, object given" when i try to json_decode in blade file here is my response data
{
Collection {#478 ▼
#items: array:5 [▼
0 => modifyHistory {#479 ▼
#attributes: array:8 [▼
"id" => 8
"user_id" => 91
"modified_id" => 6
"modified_type" => "App\Thread"
"before" => "{"slug":"one-more-try-6","title":"one more try"}"
"after" => "{"slug":"one-more-try2-6","title":"one more try2"}"
"created_at" => "2018-04-17 14:45:06"
"updated_at" => "2018-04-17 14:45:06"
]
#original: array:8 [▶]
}
}
and here is my blade code
@forelse ($history as $key => $modify)
<li class="list-group-item">
<div class="level">
<tr>
<td> {{json_decode($modify->before)}} </td>
</tr>
</div>
</li>
@empty
<strong>There is no updates for this thread </strong>
@endforelse
try this
@forelse($history as $key => $modify)
<li class="list-group-item">
<div class="level">
<tr>
<?php
$before = json_decode($modify->before)
$ttitle = $before->title;
?>
<td> {{ $title }} </td>
<!--
or you could just do
{{ $before->title }}
or
{{ $before->slug }}
-->
</tr>
</div>
</li>
@empty
<strong>There is no updates for this thread </strong>
@endforelse
@jcmargentina i did before and got this error Undefined property: stdClass::$before
debug with this code please
@foreach($history as $key => $modify)
<?php
var_dump($modify);
?>
@endforeach
The problem with blade accept an only string and you give it an array, You can decode the json then forloop and print.
@jcmargentina
Modify is a object contains my attributes
Two of them "before " , "after" in this format
I am tring to json_decode them
"before" => "{"slug":"one-more-try-6","title":"one more try"}" "after" => "{"slug":"one-more-try2-6","title":"one more try2"}"
use this instead
@foreach ($history as $modify)
Why don't you retrieve your 8 attributes when var_dump($modify), such as
id, user_id, modified_id, modified_type, ... ?
Try var_dump(json_decode($modify->before)), if it's null it's maybe an encoding issue.
What makes you think its json?
Looks like a collection to me?
<td> {{$modify->before->title ?? 'no title here' }} </td>
@Vilfago
I fetch my 8 attrributes my before and after attribute saved as string i need to decode them and when i decode i got this error
@Snapey before attribute saved as string can't fetch the title until i decode it ,
And when i use json_decode i got this error
So before using json_decode it's a string, and after json_decode it's an object ?
Really strange to me... maybe try this
@php
$before = json_decode($modify->before, true);
$title = $before['title'];
@endphp
<td> {{ $title }} </td>
The problem with json_decode function
Without using it there is no errors but it's string not array
It's why I add the second parameter, to decode in an array instead of an object... just to see, even if I don't understand what fail.
@Vilfago i will try it :) thanks for your help
Let us know, and if it's not working, var_dump $before ;)
@Vilfago same error ...
Can't var_dump $before cuz there is a error "htmlspecialchars() expects parameter 1 to be string, array given"
here is the var_dump for $modify
object(App\modifyHistory)#479 (25) { ["guarded":protected]=> array(0) { } ["connection":protected]=> string(5) "mysql" ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(8) { ["id"]=> int(8) ["user_id"]=> int(91) ["modified_id"]=> int(6) ["modified_type"]=> string(10) "App\Thread" ["before"]=> string(48) "{"slug":"one-more-try-6","title":"one more try"}" ["after"]=> string(50) "{"slug":"one-more-try2-6","title":"one more try2"}" ["created_at"]=> string(19) "2018-04-17 14:45:06" ["updated_at"]=> string(19) "2018-04-17 14:45:06" } ["original":protected]=> array(8) { ["id"]=> int(8) ["user_id"]=> int(91) ["modified_id"]=> int(6) ["modified_type"]=> string(10) "App\Thread" ["before"]=> string(48) "{"slug":"one-more-try-6","title":"one more try"}" ["after"]=> string(50) "{"slug":"one-more-try2-6","title":"one more try2"}" ["created_at"]=> string(19) "2018-04-17 14:45:06" ["updated_at"]=> string(19) "2018-04-17 14:45:06" } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["events":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
Why don't you have the same object as before ?
Did you var_dump it after another operation?
Another idea to try... because I'm desperate :
@php
dump($modify->before);
$before = $modify->before;
dump($before);
$json_before = json_decode($before, true);
dump($json_before);
@endphp
{{ $json_before['slug'] }}
use (as an object)
{{ json_decode($modify->before)->title }} <br />
{{ json_decode($modify->before)->slug }}
or (as an array)
{{ json_decode($modify->before,true)['title'] }} <br />
{{ json_decode($modify->before,true)['slug'] }}
Thanks @Vilfago for your help :)
And nah i am not var_dump after another operation
Please sign in or create an account to participate in this conversation.