0

I am wanting to explode my decoded JSON so I can remove the spacing and call certain values in a table. I am confident I need to use explode to achieve what I am after it's my first time using it so I am unsure exactly how to work it

My controller is:

public function index($id)
{

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

    var_dump($content);

    explode(',',',{,},[,]');

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

I have a standard table of

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

I have a vardump currently on the controller so I can see what is being parsed through (and to test that my decode was working). It is returning the following:

array(2) { ["text"]=> string(4) "test" ["suggestions"]=> array(1) { [0]=> array(1) { ["reply"]=> array(2) { ["text"]=> string(4) "test" ["postbackData"]=> string(36) "e05ad1f0-a0f8-4a56-a8c7-67f56ba9fe4c" } } } }

I am wanting to explode this so I can extract each of them into different columns in my table

2 Answers 2

2

So its best to use accessors in this kind of situation. You can find the documentation on how they work here https://laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor

In our Message model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Message extends Model
{

    public function getContentAttribute($value)
    {
        return json_decode($value);
    }
}

in your controller

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

Your View

<p>{{property_exists($message->content, 'text') ? $message->content->text:property_exists($message->content, 'log') ?  $message->content->log :property_exists($message->content, 'data')? $message->content->data: ""}}</p>
Sign up to request clarification or add additional context in comments.

9 Comments

I tried this and it worked for straight text ones but gives me an error if I have one of my alternatives such as logs or data, I get this error: Undefined property: stdClass::$text (View: master\resources\views\details.blade.php)
So you will need to write a condition to handle that case. So something like property_exists($message->content, 'text') ? $message->content->text: property_exists($message->content, 'log') ? $message->content->log : property_exists($message->content, 'data')? $message->content->data: ""
I updated my answer to better handle your condition of different content types.
I noticed that you updated it, it was really helpful. so I add $message->content->log :property_exists($message->content, '') for each property for each column?
yeah you will need to check which property exists so you can call the correct one so you will not get the property doesn't exist error.
|
2

In your controller

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

return view('details', ['message' => $message, 'content' => $content]); // $message is `Message::class` and $content is an `array`

blade file

<td>{{ $message->type }}</td>
<td>{{ $content['text'] }}</td>

4 Comments

@ASP.NETMVCStudentProgrammer You can use (isset($content['text'])) and then proceed
its actually returning true or false. 0 or 1. Thats becuase you must use a condition such as {{isset($content['text']) ? $content['text']:''}}
Oh okay so it's returning a bool, it didn't show any number of the 0 ones so I didn't know
Okay I see, it is working when showing the first for example: "Enjoy free Wifi" and the return response is "GIMMEE!!!". Here is the vardump: array(2) { ["text"]=> string(15) "Enjoy free wifi" ["suggestions"]=> array(1) { [0]=> array(1) { ["reply"]=> array(2) { ["text"]=> string(9) "GIMMEE!!!" ["postbackData"]=> string(36) "4f4ccd81-8bc7-4a59-90c3-effa80aec2d1" } } } } If I add <td>{{ $content['reply'] }}</td> I get an Undefined index error, what would be the correct call for this column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.