重要:我不是从模型中获取数据,而是从查询生成器查询中获取。
以下第一个函数根据用户搜索条件构建视图并相应地过滤数据:
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');
}
我已经完成了家庭作业,并试图在现有的stackoverflow问题和其他论坛中找到解决方案,但是其中大多数涉及基于模型的数据。我现在必须使用基于查询生成器的功能,并且无法切换到基于模型的解决方案]
我也试图将$ 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');
}