laravel 5.3-在刀片模板中显示来自mysql文本字段的json数据

hah*_*ute 5 php notifications json blade laravel-5.3

我在mysql text字段中有此数据:

{"message":"New comment from pp on one of your photos.","action":"\/photos\/9372"}
Run Code Online (Sandbox Code Playgroud)

使用此功能(laravel通知)将其写入数据库:

public function toArray($notifiable)
{
    return [
        'message' => 'New comment from '.Auth::user()->username.' on one of your photos.',
        'action' => '/photos/'.$this->comment->photo_id
    ];
}
Run Code Online (Sandbox Code Playgroud)

文件说:

"The returned array will be encoded as JSON and stored in the data column of your notifications table."
Run Code Online (Sandbox Code Playgroud)

然后我像这样获取数据:

$notifications = Auth::user()->notifications()->paginate(30);

return view('home.index')->with('notifications',$notifications);
Run Code Online (Sandbox Code Playgroud)

如何json array在刀片视图中显示此(?)的内容?我可以var_dump(),但不能显示。总是undefinded index或其他错误。

@forelse($notifications as $n)
    @php
        $data = $n->data;
    @endphp
    {!! var_dump($data) !!}
@empty
    nothing
@endforelse
Run Code Online (Sandbox Code Playgroud)

编辑

当我尝试json_decode数据时,它告诉我,它是一个数组:

{!! var_dump(json_decode($data)) !!}

json_decode() expects parameter 1 to be string, array given
Run Code Online (Sandbox Code Playgroud)

print_r() 说的一样:

{!! print_r($data) !!}

Array ( [message] => New comment from pp on one of your photos. [action] => /photos/9372 ) 1 
Run Code Online (Sandbox Code Playgroud)

var_dump() 说这是一个数组:

{!! var_dump($data) !!}

array(2) { ["message"]=> string(42) "New comment from pp on one of your photos." ["action"]=> string(12) "/photos/9372" } 
Run Code Online (Sandbox Code Playgroud)

但是当我想显示某个值时,它不起作用:

{!! $data["message"] !!}

Undefined index: message
Run Code Online (Sandbox Code Playgroud)

小智 0

当您尝试访问数组的消息部分时,请确保您正在循环访问通知数组:

@foreach ($notifications as $notification)
    {{ $notification->data['message'] }}
@endforeach
Run Code Online (Sandbox Code Playgroud)