Laravel 5.7 |将第一个函数中用于构建视图的数据/变量传递给第二个函数以生成 dompdf 文件

Laravel 5.7 | Passing data / variables used in a first function to build the view to a second function to generate a dompdf file

重要提示:我的数据不是从模型中获取的,而是从查询生成器查询中获取的。

以下第一个函数根据用户搜索条件构建视图并相应地过滤数据:

public function updateOrderList(Request $request) {

    *** Query : I didn't display the query since to long because of around 20 joins ***

    $orders->where('orders_detail_new.deleted_at', '=', NULL);
    $orders->distinct();
    $orders  = $orders->get();

    return view('labo_orders_mgt_view_index', compact('orders'));

}

$orders 返回的集合:

Collection {#2308 ▼
  #items: array:34 [▼
    0 => {#2309 ▶}
    1 => {#2311 ▶}
    2 => {#2317 ▶}
    3 => {#2310 ▶}
    4 => {#2313 ▶}
    5 => {#2314 ▶}
    6 => {#2315 ▶}
    7 => {#2318 ▶}
    etc.
  ]
}

我想使用与构建过滤后的新视图相同的 $orders 数据:

    return view('labo_orders_mgt_view_index', compact('orders'));

使用以下第二个函数生成 PDF 文件:

public function export_pdf() {

    $pdf = PDF::loadView('pdf.laboratory_list', compact('orders'));
    return $pdf->download('laboratory_list.pdf');

}

我做了功课,并尝试在现有的 Whosebug 问题和其他论坛中找到解决方案,但大多数问题都涉及基于模型构建的数据。我现在必须忍受这个基于查询生成器的功能,不能切换到基于模型的解决方案

我也曾尝试将 $orders 数据传递到会话中,但我了解到这不是正确的选择。

如果能给我一些解决这个棘手情况的建议,我将不胜感激。

也许您可以将整个 $orders 生成代码移动到另一个可重用函数中?

private function getOrders() {

    /* skipped */

    $query->where('orders_detail_new.deleted_at', '=', NULL);
    $query->distinct();
    return $query->get();
}

public function updateOrderList(Request $request) {
    $orders = $this->getOrders();
    return view('labo_orders_mgt_view_index', compact('orders'));
}

public function export_pdf() {
    $orders = $this->getOrders();
    $pdf = PDF::loadView('pdf.laboratory_list', compact('orders'));
    return $pdf->download('laboratory_list.pdf');
}