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

siemankohe's avatar

Undefined variable: hours and another

Hello, I have another problem with my site and i think this is not last problem. Problem: Undefined variable: hours code: @foreach($hours as $hour)

all the error is now on this page: http://wwlodarczyk.pl/hours

Please help because after correcting one mistake another one pops up ...

0 likes
5 replies
siangboon's avatar

double check the related controller to ensure that the hours have declared (and able to dd() the result as expected) and passing it to view correctly.. it may also good to check the value exist before the foreach loop....

Xitox's avatar

@siemankohe The problem is because variable $hours may not exists. Therefore you can double check it before the @foreach loop. you add this

{{ dd($hours) }}
@foreach ($hours as $hour)
	...
@endforeach

If there is no data, than that's mean there is something wrong in the controller that you pass $hours variable from. Maybe you send wrong variable etc. It will be more easier for us to identify your error if you could provide your controller function.

munazzil's avatar

have you passed that from controller to view something like below,

	$hours = Hour::get();

	return view('hours.view',compact('hours'));
sarimziaa1's avatar

It looks like the issue is coming directly from the page at where the $hours variable (and possibly others) is not being defined or passed before the @foreach($hours as $hour) loop runs. This usually happens when the controller doesn’t send the data to the view or the variable name doesn’t match, which explains why fixing one error leads to another showing up on the same URL—once one missing variable is corrected, the next undefined one is exposed www.bstationhd.app

martinbean's avatar

@sarimziaa1 This thread is half a decade old, and the domain in the original post doesn’t even resolve any more…

Please or to participate in this conversation.