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

_chris's avatar

decocde json inside model property

Hi all,

I have a model that includes a field in the database (called answers) where I am saving json data. Eloquent schema is below:

Schema::create('assessments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('identifier');
            $table->string('answers')->nullable(); //this is the filed where json data is saved
            $table->timestamps();
            $table->datetime('completed_at')->nullable();
            $table->softDeletes();
        });

When I retrieve one of these items from the database and send it to a view, the answers field is not parsed as json but kept as a string. Like so:

{
"id": 4,
"user_id": "1",
"identifier": "test",
"answers": "[{\"question\":{\"id\":\"question1\",\"title\":\"Question 1\"},\"answer\":\"test answer\"},{\"question\":{\"id\":\"question2\",\"title\":\"Question 2\"},\"answer\":\"Yes\"}]",
"created_at": "2016-04-20 08:51:21",
"updated_at": "2016-04-21 14:14:56",
"completed_at": null,
"deleted_at": null
}

Because of this, I am finding it difficult to parse the answers as json in javascript. Is there a way to easily do this?

Thanks!

0 likes
7 replies
bashy's avatar

Hi,

I did this (which is similar need) for "extra" POST data for an API system that received leads.

public function setFullDataAttribute($value)
{
    $this->attributes['full_data'] = json_encode($value);
}

public function getFullDataAttribute($value)
{
    return json_decode($value, true);
}

View

@foreach ((array) $lead->full_data as $key => $value)
    <tr>
        <td>{{ $key }}</td>
        <td>{{ $value }}</td>
    </tr>
@endforeach
_chris's avatar

@Prez thanks. I have that now. In my controller I am doing the following:

$answers = json_decode($assessment->answers);
$assessment->answers = $answers;
return $assessment;

Which seems to get the data as json but I am still having trouble getting it into javascript.

_chris's avatar

@bashy thanks. I have that now and am able to access the data in php but not javascript

I've tried the following:

var assessment = "{{ $assessment }}";
console.log(assessment);

which gives me:

{&quot;id&quot;:4,&quot;user_id&quot;:&quot;1&quot;,&quot;identifier&quot;:&quot;qazxsw&quot;,&quot;answers&quot;:[{&quot;question&quot;:{&quot;id&quot;:&quot;question1&quot;,&quot;title&quot;:&quot;Question 1&quot;},&quot;answer&quot;:&quot;test&quot;},{&quot;question&quot;:{&quot;id&quot;:&quot;question2&quot;,&quot;title&quot;:&quot;Question 2&quot;},&quot;answer&quot;:&quot;Yes&quot;}],&quot;created_at&quot;:&quot;2016-04-20 08:51:21&quot;,&quot;updated_at&quot;:&quot;2016-04-21 14:14:56&quot;,&quot;completed_at&quot;:null,&quot;deleted_at&quot;:null}

And this:

var assessment = "{!! $assessment !!}";
console.log(assessment);

Which gives me:

Uncaught SyntaxError: Unexpected identifier

robgoodliffe's avatar

Can you not use the $casts property on Eloquent class to automatically decode the string, then just return the data using the return response()->json($data); helper.

class Assessments extends Model
{
    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'answers' => 'json',
    ];
}
_chris's avatar

@Prez yes thank you! I've marked it as correct.

I should add I needed to add json_encode when I saved the data. Otherwise I got an error back.

$assessment->answers = json_encode($request->data);
bashy's avatar

Did you not just need this?

var json = $.parseJSON({{ $assessment }})
console.log(json.someitem)
1 like
_chris's avatar

@bashy no, I got an error when not escaping the characters:

Uncaught SyntaxError: Unexpected token &

Please or to participate in this conversation.