Laravel将视图合并为紧凑

时间:2018-03-03 11:14:46

标签: laravel

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

1 个答案:

答案 0 :(得分: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;