John Davidson

php - Undefined variable Issue with laravel blade

0 comments
Message:


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())

    @foreach($region_options as $region)
  • {{ $region->name }}

  • @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

3 of 5 based on 1217 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us