Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't pass variables to template #15

Closed
netm opened this issue Nov 28, 2013 · 9 comments
Closed

Can't pass variables to template #15

netm opened this issue Nov 28, 2013 · 9 comments

Comments

@netm
Copy link

netm commented Nov 28, 2013

Love this package, works great but for some reason I get an error when trying to pass to the template

In controller:

$order = Order::find($id);
$pdf = PDF::loadView('pdf.invoice', $order)->setPaper('a4')->setOrientation('portrait');
return $pdf->stream('invoice.pdf');

In the template:

{{$order->id}}

And I get an error:

Method Illuminate\View\View::__toString() must not throw an exception

If I remove the {{$order->id}} from the template the PDF works perfectly. Any help much appreciated - I must have misunderstood something..again... :)

@GrahamCampbell
Copy link
Contributor

I don't think this error is related to that by looking at https://github.com/barryvdh/laravel-dompdf/blob/master/src/Barryvdh/DomPDF/PDF.php#L140.
Check the $order you are passing to it is ok. Also, you could try passing array('order' => $order).

@netm
Copy link
Author

netm commented Nov 28, 2013

Hmmm... it must be something in the $order, it's a bit messy but it passes the lint test though:

{
    "id": "11",
    "user_id": "2",
    "address": "{\"id\":\"2\",\"user_id\":\"2\",\"name\":\"Gwilym\",\"postal_line_1\":\"100 Abel Smith Street\",\"postal_line_2\":\"Te Aro\",\"postal_line_3\":\"Wellington 6011\",\"postal_line_4\":\"\",\"postal_line_5\":\"\",\"suburb\":\"Te Aro\",\"city\":\"Wellington\",\"postcode\":\"6011\",\"region\":\"Wellington Region\",\"rd_number\":\"0\",\"created_at\":\"2013-11-28 22:49:37\",\"updated_at\":\"2013-11-28 22:49:37\",\"deleted_at\":null}",
    "details": "[{\"product\":\"65mm Standard 8 Square Drive Screws\",\"quantity\":4,\"per\":\"100\",\"weight\":1.76,\"price_ex_tax\":\"10.60\",\"price_inc_tax\":\"12.19\"}]",
    "freight_ex_tax": "4.30",
    "total_ex_freight_and_tax": "10.60",
    "total_inc_freight_and_tax": "17.14",
    "status": "Live",
    "tracking": "",
    "created_at": "2013-11-28 22:58:02",
    "updated_at": "2013-11-28 22:58:02",
    "deleted_at": null
}

@GrahamCampbell
Copy link
Contributor

If you can't get it to work, you could pass array('order' => $order->toArray()) and then use {{ $order['id'] }} in the view. Also, check you are selecting the correct columns when you hit the database.

@netm
Copy link
Author

netm commented Nov 28, 2013

Yep - I tried all of that. But just solved it - was because 'compact' was missing from the template call... so this works...

$pdf = PDF::loadView('pdf.invoice', compact('order'))->setPaper('a4')->setOrientation('portrait');
return $pdf->stream('invoice.pdf');

Thanks for your help Graham :)
``

@netm netm closed this as completed Nov 28, 2013
@barryvdh
Copy link
Owner

Well, array('order' => $order) should be the same as compact('order'), but glad this is fixed.

@GrahamCampbell
Copy link
Contributor

Indeed. I prefer to use the longer array('order' => $order) syntax for clarity.

@Penderis
Copy link

Just to clarify , this means the $data parameter must be a associative array?

@barryvdh
Copy link
Owner

Yeah it's just the same as a normal Laravel view..

@lebafu
Copy link

lebafu commented Jul 10, 2019

netm bieeen ntm gracias :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants