0

Laravel 5.8
Voyager 1.2

I'm trying to access a variable in an included file from the parent blade view.

<!-- parent.blade.php -->
@include('vendor.voyager.test.child')
{{$i}} 

<!-- child.blade.php -->
@for($i=1; $i<5; $i++)
   {{$i}} 
@endfor

I'm expecting $i would be 4 in the parent.blade. Instead I got Undefined variable: i. Any help?

Flame
  • 6,663
  • 3
  • 33
  • 53
Jim
  • 1
  • 1
  • 1
  • Where does `$i` come from? You need to assign it to the view. Your code shows you trying to display `{{$i}}` but the for-loop resets the $i variable to `1`. – Flame Aug 09 '19 at 13:33
  • `$i` was newly created inside the included file (`child.blade.php`) while iterating array I pass to to the included file – Jim Aug 09 '19 at 13:57
  • i dont think variables can go up in scope. What you define in `@include` does not become available in your parent template. It works the other way around though. – Flame Aug 09 '19 at 14:00
  • note that blades `@include` is not related to php's `include`. – Flame Aug 09 '19 at 14:01

0 Answers0