Laravel将数组分为表字段 [英] Laravel separate array into table fields

查看:42
本文介绍了Laravel将数组分为表字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,数据已传递到该表并正在存储.我想将其分为表的不同列,以使其易于阅读.

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.

当我使用时:

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

我收到错误消息:试图访问类型为null的值的数组偏移量(视图:VIEWSDIRECTORY).

这是似乎正在拉动的转储,我试图返回"text":"SPOCK" ,因为我可以为每个部分重复该过程,即键入:

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
  ]

我的控制器:

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

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

刀片:

            <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>

我的消息模型:

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

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

推荐答案

看起来(问题中不清楚)是您发布的JSON是 content 字段的值消息模型,对不对?如果是这样,则您试图直接在 Response 属性内访问 text .

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.

因此您的视图应为:

<td>{{$message->content->Response->text}}</td>

这篇关于Laravel将数组分为表字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆