got this error "htmlspecialchars() expects parameter 1 to be string, object given" when i try to json_decode in blade file

Published 4 months ago by afghany

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
jcmargentina

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

afghany

@jcmargentina i did before and got this error Undefined property: stdClass::$before

jcmargentina

debug with this code please

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

<?php
    var_dump($modify);
?>

                  
@endforeach 

hamdallah

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

afghany

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

jcmargentina

use this instead

@foreach($history as $modify)

afghany

@jcmargentina same error ?

Vilfago

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
Snapey
3 months ago (959,275 XP)

What makes you think its json?

Looks like a collection to me?

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

@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

@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

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

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

Vilfago

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.

afghany

@Vilfago i will try it :) thanks for your help

Please sign in or create an account to participate in this conversation.