Bradley Kirkland
Bradley Kirkland

Reputation: 682

Decode JSON from server in laravel

I have a JSON file that needs decoding as it has {"Text" and then the actual message I send, inside my DB it stores under "content". I am trying to decode it to have clean data inside my table.

I have tried to make my controller decode it but it doesn't seem to do anything nor give me an error. I have attached a photo so you can see what I mean. I am not getting any errors with my controller but it just doesn't seem to have any effect either. I need to define the variable content inside my controller but if I change from return view details, message => $message to details, content => content I get non-object errors (Since decode it is now an array).

My Controller:

<?php

namespace App\Http\Controllers;

use App\Message;
use App\Suggestion;
use Carbon\Carbon;
use Google\Auth\ApplicationDefaultCredentials;
use Google\Cloud\PubSub\PubSubClient;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Storage;
use Propaganistas\LaravelPhone\PhoneNumber;

class DetailsController extends BaseController
{
    public function index($id)
    {

        $message = Message::find($id);
        $content = json_decode($message->content,TRUE);

        return view('details', ['message' => $message]);

    }
}

`

My Blade file is just a table which most important part is:

<td>{{$message->type}}</td>
<td>{{$message->content}}</td>
<td>{{$message->response}}</td>
<td>{{$message->id}}</td>

Once I can convert this into an array I can explode it into more respectable columns, I have attached an image of the UI so you can see what I'm trying to do

UI image showing where content is

Upvotes: 2

Views: 472

Answers (1)

Gus Costa
Gus Costa

Reputation: 619

I am looking at the image you attached, it seems you want to display the raw JSON on the column. If that's the case, you don't need to decode it. Keep content as it is and pass $mesage to the view.

Check my code below:

    public function index($id)
    {
        $message = Message::find($id);
        return view('details', ['message' => $message]);
    }

No need to change the template.

<td>{{$message->type}}</td>
<td>{{$message->content}}</td>
<td>{{$message->response}}</td>
<td>{{$message->id}}</td>

Upvotes: 1

Related Questions