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

karimali1337's avatar

Json decode and encode

i'm storing data as json object in database like this

            'custom_field'=>json_encode(['teacher_description'=>$request->custom_field])

i want to show this data but when i do this in blade it gives this error blade

     {{json_decode($teacher->custom_field)}}

error

htmlspecialchars(): Argument #1 ($string) must be of type string, stdClass given

for example i want if i stored hello in the teacher_description i write this in blade to get hello message any hint ?

 {{json_decode($teacher->custom_field->teacher_description)}}
0 likes
12 replies
Sinnbeck's avatar

What is the query? And can you show the model?

karimali1337's avatar

@Sinnbeck

    public function show(User $teacher)
    {
        return view('manager.teachers.show',compact('teacher'));
    }

what in the model you wanna see.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@karimali1337 Try this. First decode, then get teacher_description

 {{json_decode($teacher->custom_field)->teacher_description}}
1 like
Sinnbeck's avatar

But be aware that it will fail if teacher_description is anything but a string. If its an array, you will need a foreach

1 like
tykus's avatar

When you json_decode JSON from the database, you’re getting a stdClass instance (or an array if you pass true as second argument); you can’t actually echo either using Blade braces because it’s got no __toString method

tykus's avatar

@karimali1337 use the array cast in the model so you’re dealing with reliable data type.

// User model:
protected $casts = [
    'custom_field' => 'array'
];

Then index into the array:

{{ $teacher->custom_field['teacher_description'] ?? 'n/a' }}
1 like

Please or to participate in this conversation.