Laravel合并视图以紧凑



我想合并一些视图,因为我收到了不同类型的通知,我得到了 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;

最新更新