Ad

Laravel 5.7 | Passing Data / Variables Used In A First Function To Build The View To A Second Function To Generate A Dompdf File

- 1 answer

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.

Ad

Answer

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');
}
Ad
source: stackoverflow.com
Ad