Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afghany's avatar

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
0 likes
21 replies
jcmargentina's avatar

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's avatar

debug with this code please

@foreach($history as $key => $modify)

<?php
    var_dump($modify);
?>

                  
@endforeach 

hamdallah's avatar

The problem with blade accept an only string and you give it an array, You can decode the json then forloop and print.

afghany's avatar

@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"}"

Vilfago's avatar

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.

Snapey's avatar

What makes you think its json?

Looks like a collection to me?

<td> {{$modify->before->title ?? 'no title here' }} </td>
afghany's avatar

@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

afghany's avatar

@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

Vilfago's avatar

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>

afghany's avatar

The problem with json_decode function Without using it there is no errors but it's string not array

Vilfago's avatar

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's avatar

Let us know, and if it's not working, var_dump $before ;)

afghany's avatar

@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) { } }

Vilfago's avatar

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'] }}
Snapey's avatar

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'] }}
afghany's avatar

Thanks @Vilfago for your help :) And nah i am not var_dump after another operation

Please or to participate in this conversation.