【问题标题】:laravel undefined index error while retrieving notification data from database从数据库中检索通知数据时,laravel未定义索引错误
【发布时间】:2017-02-07 09:56:46
【问题描述】:

我已经创建了将订单提交事件存储为的通知

 return [
       'cart_id'=>$this->cart->id,
       'text'=>'order is submitted'
    ];

当我从数据库中检索通知数据时

 @foreach (Auth::user()->unreadNotifications as $notification ) 
   {{$notification->data['text'] }}
 @endforeach 

这会作为未定义的索引文本给出错误

但如果我尝试通过

访问文本
@foreach ($notification->data as $key => $data)
{{$data}}
@endforeach 

有效

但是为什么我不能通过$notification->data['text']访问数据

【问题讨论】:

    标签: php laravel laravel-5.3


    【解决方案1】:

    您应该直接获得text 属性,例如:

    @foreach (Auth::user()->unreadNotifications as $notification )
        {{ $notification->text }}
        //Or use
        {{ $notification['text'] }}
    @endforeach 
    

    希望这会有所帮助。

    【讨论】:

    • 好的,请尝试$notification->data->text
    • 也试试json_decode( $notification->data )->text
    猜你喜欢
    • 2020-04-24
    • 2018-10-10
    • 1970-01-01
    • 2019-08-10
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2011-02-10
    相关资源
    最近更新 更多