Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

robtcallahan's avatar

How to access controller-passed var in php block in view

Hi folks, I have a form that passes page dimensions from a controller to a view and want to be able to perform different logic bases upon those dimensions. Essentially, the view formats the output in 3 columns per page and I need to know when to trigger a new page which is based on the specified page height.

The problem is that vars passed to the view by the controller only seem to be able to be used for HTML output and I cannot use them in PHP logic. I figured that there must be a way to do this in an elegant way. I would appreciate any suggestions you may have. Thanks, Rob

0 likes
5 replies
robtcallahan's avatar

@vincent15000 I understand how to embed PHP into the view. What I want to do is access a controller-passed variable in a PHP block. So, in my controller I have

return view('pdf', compact('days', 'height'));

then in pdf.blade.php, I want to access $height in a PHP block such as

@php
	$num_lines = $height * 96;
@endphp
1 like
robtcallahan's avatar

@vincent15000 you are correct! My bad. I made the false assumption that , because PHPStorm declared that $height was an unknown variable, it would not work. But indeed, it works perfectly. I'm sorry to take up your time. And thank you!

1 like

Please or to participate in this conversation.