2

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

10
  • 1
    what is working / what is not working / what are you attempting to get working? Commented May 11, 2020 at 23:42
  • i'll tidy my question, I want to decode the JSON file so it shows the data inside the table separated instead of with all the { and [ Commented May 11, 2020 at 23:46
  • 1
    Are you having problem with this line $body = json_decode($response->getBody(),true);? The $response->getBody() part seems wrong, from where is this data coming from? You have a return on the line before this, so this code won't be executed. Commented May 11, 2020 at 23:48
  • I think that is where the issue is, the data comes from a Google API which creates the JSON and stores it inside the MYSQL DB which I'm hosting through XAMPP, the table is Messages and has a field named content which is where the values are coming from Commented May 11, 2020 at 23:52
  • What I need to do is pull them from the DB as they are for example: "{"text":"message","suggestions":[{"reply":{"text":"test"}", decode it to be message, test Commented May 11, 2020 at 23:55

1 Answer 1

1

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> 
Sign up to request clarification or add additional context in comments.

10 Comments

Great minds think alike, this is the same as what I came to. The issue is I am trying to pull it from Google not the DB I changed it so that I am instead using the DB by $message = Message::find($id); $content = json_decode($message->content,TRUE); print_r($content); return view('details', ['message' => $message]); } }
In this case, it shouldn't be return view('details', ['content' => $content]); } }?
No it still loads with return view('details', ['message' => $message]); But the changes aren't implemented when running return view('details', ['content' => $content]); it gives an undefined variable error, do you know if any changes need to be made to the blade to make it work? If so what? `
returning the view return view('details', ['content' => $content]); is an unreachable statement problem
The variable name on the template should match the name you defined on view(). In this case <td>{{$content->type}}</td>.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.