John Davidson

php - Undefined variable Issue with laravel blade


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,

<ol class="region-pop">
@foreach ($region_options as $key => $region)
<li>{{ $region->name }}</li>

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:

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

  • @endforeach


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

5 of 5 based on 9250 votes


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