In my laravel application, I'm having a list of regions in my DB
This is my controller method
public function index()
{
$region_options = Region::get();
return view('home',compact('region_options'));
}
and I have following in my blade,
@if(!$region_options->isEmpty())
<ol class="region-pop">
@foreach ($region_options as $key => $region)
<li>{{ $region->name }}</li>
@endforeach
</ol>
@endif
But when I run this it gave me an error saying, Undefined variable: region_options
I'm struggling to find what the issue is...
My Answer:
>isEmpty())
- {{ $region->name }}
@foreach($region_options as $region)
@endforeach
@endif
In this code, the issue is that the variable $region_options is not defined in the blade file. To fix this issue, you need to pass the variable from the controller to the view using the compact function. Make sure that the variable name is the same in both the controller and the blade file.
Rate this post
4 of 5 based on 5480 votesComments