简体   繁体   English

Laravel合并视图以压缩

[英]Laravel merge views to compact

I'm want merge some views, because I got different types of notification and I got the HTML and each view... How I can make it? 我想要合并一些视图,因为我得到了不同类型的通知,并且我得到了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'));
}

You can use the render method on the view to get it as a string and then save it to a variable for later use. 您可以在view上使用render方法将其作为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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM