1

Hi everyone I am facing an issue

I was using this layout architecture before and facing this issue

in master.blade.php

code.....

@php $foo = "bar"; @endphp

@yield('main')

code ends....

in contact.blade.php

@extends('master')

code...

@section('main')

@php echo $foo; @endphp

@endsection

code ends...

ERROR: undefined varibale foo in contact.blade.php

.......................................................................

then I switched to the new architecture and again I am facing the same issue.

in master.blade.php

code.....

@php $foo = "bar"; @endphp

code ends....

in contact.blade.php

code...

@includes('master')

@php echo $foo; @endphp

code ends...

ERROR: undefined varibale foo in contact.blade.php

So suggest me what I have to do to access the variable.

  • you should have a look at the generated php files in `storage/framework/views` and try to understand what it is actually doing behind the scenes. It will make a lot more sense – Cornel Raiu Sep 30 '20 at 09:44
  • Hi @CornelRaiu thanks for you reply but if there is any thing I am doing wrong ? – Mubashir Khan Sep 30 '20 at 09:50
  • Does this answer your question? [Laravel Blade passing variable with string through @include causes error](https://stackoverflow.com/questions/29739745/laravel-blade-passing-variable-with-string-through-include-causes-error) – Cornel Raiu Sep 30 '20 at 12:09
  • https://laravel.com/docs/7.x/blade#including-subviews – Cornel Raiu Sep 30 '20 at 12:14
  • No, this link shows that how to pass data into the @include – Mubashir Khan Sep 30 '20 at 12:57
  • check this one out :) https://laracasts.com/discuss/channels/laravel/laravel-blade-variables-into-atyield . Google: "laravel blade pass variable to yield" and it will give you a series of interesting results. – Cornel Raiu Sep 30 '20 at 13:56
  • blade content created from view factory, the data pass the factory and rendered its content as echoed. It is not like the php include. So if you define variable like this, it will be echoed and variable will be undefined – STA Oct 01 '20 at 18:43

1 Answers1

0

Add @php global $foo; @endphp

in all your blades.

Solve my problem as I'm using a variable in my sub-blade to control what javascript to include (avoiding duplicate) in master.

Luuk
  • 12,245
  • 5
  • 22
  • 33
Yan Zhao
  • 359
  • 4
  • 15