mafortis
mafortis

Reputation: 7128

Getting data out of array column in laravel 5.5

I save my order data to notifications table as my notification using laravel Notification and I have problem with returning my data.

here is my notification method: App\Notifications\OrderSubmited.php

public function toArray($notifiable)
    {
        return [
            'order_id' => $this->order->id,
            'title' => $this->order->title,
            'buyer' => $this->order->user_id,
            'status' => $this->order->orderstatus_id,
        ];
    }

My method to get data:

View::composer('admin.dashboard', function ($view) {
          $notifications = DB::table('notifications')->orderby('id', 'desc')
              ->latest()
              ->take(5)
              ->get();
          $notifs = $notifications;
          $view->with('notifs', $notifs);
        });

here is my blade code:

@foreach($notifs as $notif)

  {{dd($notif)}}

  @foreach($notif->data as $data)
    {{$data['order_id']}} <br>           
  @endforeach

@endforeach

this is my {{dd($notif)}} output:

{#721 ▼
  +"id": "46546547464415474884"
  +"type": "App\Notifications\OrderSubmited"
  +"notifiable_id": 2
  +"notifiable_type": "App\Order"
  +"data": "{"order_id":2,"title":"tsdfg", "user_id":"1", "orderstatus_id":"11"}"
  +"read_at": null
  +"created_at": "2018-01-18 00:00:00"
  +"updated_at": "2018-01-18 00:00:00"
}

If I remove my dd i will get this error:

Invalid argument supplied for foreach()

On this line:

@foreach($notif->data as $data)

Any idea?

Upvotes: 1

Views: 1982

Answers (1)

sam
sam

Reputation: 5599

Each notification is an object containing an array called data containing the values you return from your toArray method when creating the notification. You would normally iterate through your notifications and access their data like so:

@foreach ($notifications as $notification)
    {{ $notification->data['order_id'] }}
@endforeach

Laravel converts the data property (which is stored in the database as JSON) into an array.

However because you are retrieving the notifications yourself from the database without making use of the built in methods you will need to convert that data to an array from JSON yourself, like so:

View::composer('admin.dashboard', function ($view) {
    $notifications = DB::table('notifications')->orderby('id', 'desc')
      ->latest()
      ->take(5)
      ->get();

    $view->with('notifications', $notifications);
});

Then from within your view:

@foreach ($notifications as $notification)
    @php $data = json_decode($notification->data, true); @endphp
    {{ $data['order_id'] }}
@endforeach

Regarding your follow up question, something like this:

View::composer('admin.dashboard', function ($view) {
    $notifications = DB::table('notifications')->orderby('id', 'desc')
      ->latest()
      ->take(5)
      ->get()
      ->map(function ($item, $key) {
          $item->data = json_decode($item->data);
          $item->data->user = User::find($item->data->user_id);
          return $item;
      });

    $view->with('notifications', $notifications);
});

Then from within your view:

@foreach ($notifications as $notification)
    {{ $notification->data->user->name }}
@endforeach

Upvotes: 1

Related Questions