mpdflaravel-10

Laravel Mccarlosen/Laravel-mpdf package unable to pass data to blade template


I can not get object data to pass to Blade template with Mccarlosen/Laravel-mpdf Package. The desire is to pass a singe row database record but call to page is met with error - "undefined variable $agency"

Target blade data syntax is correct as follows -

{{ $agency->id }}

Instructions per the repo as follows -

    use PDF;

class ReportController extends Controller 
{
    public function viewPdf()
    {
        $data = [
            'foo' => 'bar'
        ];

        $pdf = PDF::loadView('pdf.document', $data);

        return $pdf->stream('document.pdf');
    }

}

In my controller, I am calling the Object without failure, grabbing the data without failure, and streaming pdf correctly without data (all tested). But when $agency is chained according to example above (per the common Laravel syntax) it fails. I believe this might be secondary to nested structure of data in Laravel object. But not sure. My controller function is below.

public function print(string $id) {

        // Read a single row of data in the Model
        $agency = Agency::find($id);

        // Redirect to the view with the data
        $pdf = PDF::loadView('modules.agencyMain.print',$agency);

        return $pdf->stream('agency.pdf');

    }

Looking for guidance/answers.


Solution

  • The agency has to be a key in the passed data:

    $pdf = PDF::loadView('modules.agencyMain.print', ['agency' => $agency]);
    

    As you would do in a plain Blade template: https://laravel.com/docs/10.x/blade#displaying-data