DevTaabi
DevTaabi

Reputation: 77

Undefined index: user when I display notification json array in laravel

When try to display the json data it gives the Undefined index: user exception

  foreach(auth()->user()->unreadnotifications as $notifications)

       @include('frontend.common.notification.'.snake_case(class_basename($notifications->type)))

  @endforeach

Included view code is

  <div>
    <i class="fa fa-comment fa-fw"></i>
    <img src="{{url('images/account/'.$notifications->data['user']['currentimage'])}}" height="50px" width="50"/>
    {{$notifications->data['user']['email']}} commented on your post.
    <span class="pull-right text-muted small">{{$notifications->created_at->diffForHumans()}}</span>
</div>

Json Object is

{"comment":{"comment":"I am awesome","user_id":2,"post_id":"2","commment_rate":90,"updated_at":"2017-07-06 16:32:07","created_at":"2017-07-06 16:32:07","id":18},"user":{"id":2,"email":"[email protected]","currentimage":"1499004938.jpg","role":0,"verified":null,"blocked":null,"created_at":"2017-07-02 13:44:24","updated_at":"2017-07-02 14:15:38"}}

Upvotes: 0

Views: 422

Answers (1)

FULL STACK DEV
FULL STACK DEV

Reputation: 15941

After looking at your code I assume you are using foreach loop on the collection.

  <div>
<i class="fa fa-comment fa-fw"></i>
<img src="{{url('images/account/'.$notifications->user->currentimage}}" height="50px" width="50"/>
{{$notifications->user->email}} commented on your post.
<span class="pull-right text-muted small">{{$notifications->created_at->diffForHumans()}}</span>

Upvotes: 0

Related Questions