0

I have a table, data has been passed to it, and is being stored. I want to separate it into different columns of a table to make it easily readable.

When I use:

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

I get the error: Trying to access array offset on value of type null (View: VIEWSDIRECTORY).

This is the dump of what seems to be pulling, I am trying to return the "text":"SPOCK" as I can then repeat the process for each one section i.e type:

 {
        #attributes: array:11 [
    "id" => "b5ef7556-c208-40b0-8bfa-1358bf482cd0"
    "method" => "sms"
    "msisdn" => 6422
    "direction" => "mo"
    "type" => "suggestion"
    "status" => "received"
    "content" => "{"senderPhoneNumber":"+6422","messageId":"Ms5ppMnxRHTw26gFSRwbsvAA","sendTime":"2020-06-05T03:20:58.506749Z","suggestionResponse":{"postbackData":"49da99a5-bc85-4efd-9587-54c335e7f329","text":"SPOCK","type":"REPLY"}}"
    "suggestion_id" => "49da99a5-bc85-4efd-9587-54c335e7f329"
    "created_at" => 1591327269
    "updated_at" => 1591327269
    "deleted_at" => null
  ]

My controller:

    {
        $message = Message::find($id);

        return view($message->direction . $message->type, compact('message'));
    }
}

Blade:

            <thead>
                <tr>
                    <th scope="col">MESSAGE ID</th>
                    <th scope="col">MESSAGE</th>
                </tr>
            </thead>

            <tbody>
            <tr>
                <td style='font-size:14px'>{{$message->id}}</td>    
            <td>{{$message->Content['text']}}</td>

My Message Model:

    /**
     * Get the suggestions for this message.
     */
    public function suggestions()
    {
        return $this->hasMany(Suggestion::class);
    }

    public function getContentAttribute($value)
    {
        return json_decode($value);
    }
8
  • Please put here $message variable value Commented Jun 5, 2020 at 5:17
  • Where do I put this, i have put $message in the <td> Commented Jun 5, 2020 at 5:42
  • Do you have any relationship in Message model ? Commented Jun 5, 2020 at 5:54
  • @KhalidKhan I have updated my question to include the message Model, there's not much to it though, all the data is linked inside the db as: {"senderPhoneNumber":"+12314","messageId":"M231dsa","sendTime":"2020-06-05T01:45:48.959577Z","Response":{"postbackData":"11612930-bb54-4e36-bfaa-8c7b7607675e","text":"TESTER TESTER ","type":"REPLY"}} Commented Jun 6, 2020 at 1:46
  • This question would be easier to answer if you could reduce the code to a minimal reproducible example - we don't need any of your script includes, checks on status, etc, and the controller code you've shown has about 17 use statements that aren't actually referenced in your sample. Since you've only got one item, even the fact that the output is an HTML table is irrelevant - just try to output one particular value, and show us that code and the exact output it gives you. Commented Jun 6, 2020 at 15:16

1 Answer 1

1

Looks like (not clear in the question) that the JSON you posted is the value of the content field of your Message model, right? If so, you are trying to access text directly while its inside the Response property.

So your view should be like:

<td>{{$message->content->Response->text}}</td>
Sign up to request clarification or add additional context in comments.

4 Comments

I just noticed that actually! I think you're right I will give this a try first thing in the morning!!! Even looking at the DB it appears to be nested inside the response { }
Okay so if I put through as <td>{{$message->content->Response->text}}</td> I get the same error but its reference to response instead of text. However if I do <td>{{$message->Response->text}}</td> I get a non-object error. Is there anyway this is helpful to getting us closer to finding fix?
Here is the official error: Trying to get property 'text' of non-object
I tried a new way and it gave some success or indicates some success if you have any ideas let me know :)

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.