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

mozew's avatar
Level 6

display JSON

I stored my checkboxes in https://www.laracasts.com/discuss/channels/laravel/how-to-store-all-checkboxes-in-laravel site. And I have a question, How to display JSON data in blade.

InspectionController.php

public function index()
{
    $inspections = Inspection::latest()->where('inspection_data', 1)->get();
    return view('Admin.infractions.all', compact('inspections'));
}

Inspection.php

public function inspections()
{
    return $this->belongsTo(Inspection::class, 'inspection_data');
}

all.blade.php

@foreach($inspections as $inspection)
    <tr>
        <td>{{ $inspection->inspection_data->id }}</td>
        <td>{{ $inspection->inspection_data->title }}</td>
    </tr>
@endforeach 

migration for infractions table:

public function up()
{
    Schema::create('infractions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->integer('score');
        $table->timestamps();
    });
}

migration for inspections table:

public function up()
{
    Schema::create('inspections', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('requisition_id')->unsigned();
        $table->string('encouragement_data')->nullable(true);
        $table->text('infraction_data')->nullable(true);
        $table->timestamps();
        
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('requisition_id')->references('id')->on('requisitions')->onDelete('cascade');
    });

I want to show title in blade.php. What should I do?

0 likes
38 replies
Cronix's avatar

Show the output of this in your view

@foreach($inspections as $inspection)
    {{ dump($inspection->inspection_data) }}
@endforeach 
mozew's avatar
Level 6

I get this error. and my laravel is 5.4

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'inspection_data' in 'where clause' (SQL: select * from inspections where inspection_data = 1 order by created_at desc)

Cronix's avatar

Ah, well you should have gotten that error before changing that then? The error doesn't have anything to do with the code I had you add because here, you're trying to select inspection_data from the inspections table

 $inspections = Inspection::latest()->where('inspection_data', 1)->get();

But your inspections table has no column named inspection_data.

 Schema::create('inspections', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('requisition_id')->unsigned();
        $table->string('encouragement_data')->nullable(true);
        $table->text('infraction_data')->nullable(true);
        $table->timestamps();
        
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('requisition_id')->references('id')->on('requisitions')->onDelete('cascade');
    });

which is exactly what your error is saying

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'inspection_data' in 'where clause' (SQL: select * from inspections where inspection_data = 1 order by created_at desc)

mozew's avatar
Level 6

Yes, I made a mistake. I changed my code but It do not display a title.

{{ dump($inspection->inspection_data) }}

And my laravel is 5.4

mozew's avatar
Level 6

I changed my code:

public function index()
{
    $inspections = Inspection::latest()->where('infraction_data', 1)->get();
    return view('Admin.list-violations-school.all', compact('inspections'));
}
Cronix's avatar

what does this show?

@foreach ($inspections as $inspection)
    {{ dump($inspection->infraction_data) }}
@endforeach 

This whole thing is confusing. The only place I see title is in the infractions table, not in the inspections table which is what you're querying. But the inspections table has a infraction_data field which (from the linked to post in your first post) appears to be a json encoded array of infractions? What's the point of that?

mozew's avatar
Level 6

It is true. But I stored the JSON and how can I display it on view?

Cronix's avatar

Are you sure you actually get any data back from your query? What does this output?

public function index()
{
    $inspections = Inspection::latest()->where('infraction_data', 1)->get();

    dd($inspections->first());

    return view('Admin.infractions.all', compact('inspections'));
}

->where('infraction_data', 1) doesn't make sense if the column is a text field containing a json encoded string.

Cronix's avatar

It's what I suspected.

How do you expect this where query to work

->where('infraction_data', 1) // WHERE infraction_data = 1

when infraction_data is a string like {"1":"0","2":"0"}?

Also, as you can see, there is no "title" or "id" stored in there.

RamjithAp's avatar

use whereNotNull.

   $inspections = Inspection::latest()->whereNotNull('inspection_data')->get();
    return view('Admin.infractions.all', compact('inspections'));

And you cannot access json data directly you have to json_decode() it on your controller or view.

   @foreach($inspections as $inspection)
    <?php $inspection_data = json_decode($inspection->inspections_data,true); ?>
    <tr>
        <td>{{ $inspection_data["id"] }}</td>
        <td>{{ $inspection_data ["title"] }}</td>
    </tr>
@endforeach 

Or do the json decode on your controller before sending it to view.

mozew's avatar
Level 6

Thanks for answer. I tried and I put this. But my problem is not solve. I get this error.

json_decode() expects parameter 1 to be string, array given

RamjithAp's avatar

Do json encode and then decode

  @foreach($inspections as $inspection)
    <?php $inspection_data = json_decode(json_encode($inspection->inspections_data),TRUE); ?>
    <tr>
        <td>{{ $inspection_data["id"] }}</td>
        <td>{{ $inspection_data ["title"] }}</td>
    </tr>
@endforeach 
RamjithAp's avatar

Where do you using this function htmlspecialchars() ?

mozew's avatar
Level 6

I replaced <?php ?> to {{ }}. but I I replaced {{ }} to <?php ?> NOW.

I get this error.

Undefined index: id

mozew's avatar
Level 6

Thank you for taking the time

RamjithAp's avatar

Can you please print and show me the data in $inspection->inspections_data.

print_r($inspection->inspections_data);
mozew's avatar
Level 6
Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 1 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 1 [14] => 0 [15] => 1 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 0 [24] => 0 [25] => 0 [26] => 0 [27] => 0 [28] => 0 [29] => 0 [30] => 0 [31] => 0 [32] => 0 [33] => 0 [34] => 0 [35] => 0 [36] => 0 [37] => 0 [38] => 0 [39] => 0 [40] => 0 [41] => 0 [42] => 0 [43] => 0 [44] => 0 [45] => 0 [46] => 0 [47] => 0 [48] => 0 [49] => 0 [50] => 0 [51] => 0 [52] => 0 [53] => 0 [54] => 0 [55] => 0 [56] => 0 [57] => 0 [58] => 0 [59] => 0 [60] => 0 [61] => 0 [62] => 0 [63] => 0 [64] => 0 [65] => 0 [66] => 0 [67] => 0 [68] => 0 ) 
RamjithAp's avatar

As you see you trying to get an object "id" but there is no such object exist in the inspection_data.

If you explain what id, title you trying to fetch with above numbers we can help.

mozew's avatar
Level 6

I replaced following line

@foreach($inspections as $inspection)
    <?php $infraction_data = json_decode(json_encode($inspection->infraction_data),TRUE); ?>
    <?php print_r($inspection->infraction_data); ?>
    <?php die(); ?>
    <tr>
        <td>{{ $infraction_data["title"] }}</td>
        <td>{{ $infraction_data["id"] }}</td>
    </tr>
@endforeach 

inspection_data is wrong. because I mean the following code:

public function index()
{
    $inspections = Inspection::latest()->whereNotNull('infraction_data')->get();
    return view('Admin.list-violations-school.all', compact('inspections'));
}
RamjithAp's avatar

Still, your infraction_data column has only numbers in it, I do not see any id or title data. So you cannot get id from that table.

mozew's avatar
Level 6

No I have infraction_data field in in inspections

JSON

RamjithAp's avatar

But id and title data not there. Where do you saving the.m.

mozew's avatar
Level 6

I have title , id,... in infractions table

JSON

RamjithAp's avatar

You cannot write relationship for json data which saved in a column. You need to use pivot table to save infractions relation. Or you have to loop through json data and write query to pull infractions details.

mozew's avatar
Level 6

Or you have to loop through json data and write query to pull infractions details.

How to do it?

Next

Please or to participate in this conversation.