我想合并一些视图,因为我收到了不同类型的通知,我得到了 HTML 和每个视图......我怎么能做到?
function index(){
$notifications = Notify::where('notifiable_id', auth()->user()->id)->take(5)->get();
foreach($notifications as $notification):
if($notification->type == 'AppNotificationsNotifyLinkOwnerComment'):
$data = json_decode($notification->data, true);
//comment
//view('site.list.header.notifications.commentlink', compact('notification', 'data'));
elseif($notification->type == 'AppNotificationsNotifyCommentOwnerReply'):
//reply
//view('site.list.header.notifications.commentreply', compact('notification', 'data'));
endif;
endforeach;
//views in compact = all views merged
return view("site.balloons.header.notifications", compact('notifications', 'views'));
}
可以在view
上使用 render
方法将其作为string
获取,然后将其保存到变量以供以后使用。
$views = '';
foreach($notifications as $notification):
if($notification->type == 'AppNotificationsNotifyLinkOwnerComment'):
$data = json_decode($notification->data, true);
//comment
$views .= view('site.list.header.notifications.commentlink', compact('notification', 'data'))->render();
elseif($notification->type == 'AppNotificationsNotifyCommentOwnerReply'):
//reply
$views .= view('site.list.header.notifications.commentreply', compact('notification', 'data'))->render();
endif;
endforeach;