从 laravel 5.5 中的数组列中获取数据
Getting data out of array column in laravel 5.5
我将我的订单数据保存到 notifications
table 作为我的通知 using laravel Notification
我在 return 处理我的数据时遇到问题。
这是我的通知方式: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,
];
}
我获取数据的方法:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get();
$notifs = $notifications;
$view->with('notifs', $notifs);
});
这是我的 blade 代码:
@foreach($notifs as $notif)
{{dd($notif)}}
@foreach($notif->data as $data)
{{$data['order_id']}} <br>
@endforeach
@endforeach
这是我的 {{dd($notif)}}
输出:
{#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"
}
- 如你所见,我尝试 return 的数据存储在
data
列中。
如果我删除我的dd
,我会得到这个错误:
Invalid argument supplied for foreach()
这一行:
@foreach($notif->data as $data)
有什么想法吗?
每个通知都是一个对象,其中包含一个名为 data
的数组,其中包含您在创建通知时 return 来自 toArray
方法的值。您通常会像这样遍历通知并访问它们的数据:
@foreach ($notifications as $notification)
{{ $notification->data['order_id'] }}
@endforeach
Laravel 将 data
属性(在数据库中存储为 JSON)转换为数组。
但是,由于您是自己从数据库中检索通知而不使用内置方法,因此您需要自己将该数据从 JSON 转换为数组,如下所示:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get();
$view->with('notifications', $notifications);
});
那么在你的视野中:
@foreach ($notifications as $notification)
@php $data = json_decode($notification->data, true); @endphp
{{ $data['order_id'] }}
@endforeach
关于你的后续问题,是这样的:
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);
});
那么在你的视野中:
@foreach ($notifications as $notification)
{{ $notification->data->user->name }}
@endforeach
我将我的订单数据保存到 notifications
table 作为我的通知 using laravel Notification
我在 return 处理我的数据时遇到问题。
这是我的通知方式: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,
];
}
我获取数据的方法:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get();
$notifs = $notifications;
$view->with('notifs', $notifs);
});
这是我的 blade 代码:
@foreach($notifs as $notif)
{{dd($notif)}}
@foreach($notif->data as $data)
{{$data['order_id']}} <br>
@endforeach
@endforeach
这是我的 {{dd($notif)}}
输出:
{#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"
}
- 如你所见,我尝试 return 的数据存储在
data
列中。
如果我删除我的dd
,我会得到这个错误:
Invalid argument supplied for foreach()
这一行:
@foreach($notif->data as $data)
有什么想法吗?
每个通知都是一个对象,其中包含一个名为 data
的数组,其中包含您在创建通知时 return 来自 toArray
方法的值。您通常会像这样遍历通知并访问它们的数据:
@foreach ($notifications as $notification)
{{ $notification->data['order_id'] }}
@endforeach
Laravel 将 data
属性(在数据库中存储为 JSON)转换为数组。
但是,由于您是自己从数据库中检索通知而不使用内置方法,因此您需要自己将该数据从 JSON 转换为数组,如下所示:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get();
$view->with('notifications', $notifications);
});
那么在你的视野中:
@foreach ($notifications as $notification)
@php $data = json_decode($notification->data, true); @endphp
{{ $data['order_id'] }}
@endforeach
关于你的后续问题,是这样的:
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);
});
那么在你的视野中:
@foreach ($notifications as $notification)
{{ $notification->data->user->name }}
@endforeach