Laravel将视图合并为紧凑

问题描述 投票:0回答:1

我想要合并一些视图,因为我得到了不同类型的通知,我得到了HTML和每个视图......我怎么能做到?

function index(){
    $notifications = Notify::where('notifiable_id', auth()->user()->id)->take(5)->get();

    foreach($notifications as $notification):
        if($notification->type == 'App\Notifications\NotifyLinkOwnerComment'):

            $data = json_decode($notification->data, true);
            //comment
            //view('site.list.header.notifications.commentlink', compact('notification', 'data'));

       elseif($notification->type == 'App\Notifications\NotifyCommentOwnerReply'):
            //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'));
}
laravel
1个回答
0
投票

您可以在render上使用view方法将其作为string,然后将其保存到变量供以后使用。

$views = '';
foreach($notifications as $notification):
    if($notification->type == 'App\Notifications\NotifyLinkOwnerComment'):

        $data = json_decode($notification->data, true);
        //comment
        $views .= view('site.list.header.notifications.commentlink', compact('notification', 'data'))->render();

   elseif($notification->type == 'App\Notifications\NotifyCommentOwnerReply'):
        //reply
        $views .= view('site.list.header.notifications.commentreply', compact('notification', 'data'))->render();
    endif;
endforeach;
© www.soinside.com 2019 - 2024. All rights reserved.