Home > Software engineering >  undefined variable in view using dompdf
undefined variable in view using dompdf

Time:05-23

I am trying to generate a pdf with DOMPDF. This is my code:

class PDFController extends Controller
{
    public function invoice(Request $request) {
        // $institution = $this->institution();
        // $user = $this->user();
        $invoice = array($this->invoice_form($request));

        $pdf = PDF::loadView('pdf-generation.invoice', $invoice);
        return $pdf->setPaper('a4', 'landscape')->download('invoice.pdf');

        //return view('pdf-generation.invoice')->with(['institution' => $institution, 'user' => $user, 'invoice' => $invoice]);
    }

    public function institution() {
        $institution = Institution::where('id', 1)->get()->first();

        return $institution;
    }

    public function user() {
        $user = Auth::user();

        return $user;
    }

    public function invoice_form(Request $request) {

        $this->validate($request, array(
            'furnizor-select' => 'required',
            'document-number' => 'required',
            'document-date' => 'required',
            'due-date' => 'required',
            'discount-procent' => 'required',
            'discount-value' => 'required',
            'total-value' => 'required',
            'nir-number' => 'nullable'
        ));

        $invoice = new \App\Models\Invoice();
        $invoice->provider_id = $request->input('furnizor-select');
        $invoice->number = $request->input('document-number');
        $invoice->document_date = $request->input('document-date');
        $invoice->due_date = $request->input('due-date');
        $invoice->discount_procent = $request->input('discount-procent');
        $invoice->discount_value = $request->input('discount-value');
        $invoice->total = $request->input('total-value');
        $invoice->save();

        $invoices = Invoice::all();
        $invoice_id = $invoices->last()->id;

        $old_date = $request->input('document-date');
        $new_date = date("d-m-Y", strtotime($old_date));  
        $provider_id = $request->input('furnizor-select');
        $provider = Provider::where('id', $provider_id)->get();
        $invoice_number = $request->input('document-number');
        $old_due_date = $request->input('due-date');
        $new_due_date = date("d-m-Y", strtotime($old_due_date));  

        $filename = 'pdfs/nir'.$invoice_id.'.pdf';

        $institution = $this->institution();
        $user = $this->user();

        $array = array(
            'invoice_id' => $invoice_id,
            'new_date' => $new_date,
            'provider' => $provider,
            'invoice_number' => $invoice_number,
            'due_date' => $new_due_date,
            'provider' => $provider,
            'institution' => $institution,
            'user' => $user
        );

        return (object) $array;
    }
}

And in my pdf-generation.invoice view, I have some html generate but it is not worth to post it all, so I am going to post only one line to give you some idea about the problem:

 <span style="font-weight: bold; float: left;">{{$invoice->institution}}</span>

However, it says Undefined variable $invoice.. what could be the problem?

CodePudding user response:

=>you can compact your variable like this.

$pdf = PDF::loadView('pdf-generation.invoice', compact('invoice'));
  • Related