-1

Important : I didn't get my data from a model, but from a Query Builder query.

The following first function build a view based on the user search criteria and filtered the data accordingly :

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'));

}

Collection returned by $orders :

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

I would like to use the same $orders data which are used to build the filtered new view :

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

to generate a PDF file with the following second function :

public function export_pdf() {

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

}

I have done my home work and tried to find a solution in existing stackoverflow questions and other forums, but most of them concern data which are build based on models. I have to live now with this Query Builder based function and can not switch to a model based solution

I have also tried to pass the $orders data into session, but I read that it was not the right choice.

I would appreciate some suggestion how to solve this tricky situation.

marcq
  • 477
  • 3
  • 12
  • Is the issue that you're returning a view with your controller, and from that view there is a link which calls a new function? – party-ring Nov 22 '19 at 11:15

1 Answers1

1

Maybe you can move whole $orders generation code into another reusable function?

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');
}
Stalinko
  • 3,319
  • 28
  • 31
  • Well received @Stalinko, thank you. I will test tomorrow, since here it is evening in Vietnam, I'll keep you posted about the outcome – marcq Nov 22 '19 at 10:57
  • That helped me a lot @Stalinko, still have an issue consecutively of this change, but I will fix it, thank you for your solution – marcq Nov 23 '19 at 05:54