Reputation: 137
I trying to set a php variable in blade. Its works but also on the same blade and not set the var on the other blades.
example:
header.blade.php
@if(isset($company))
@php ($color = $company->color)
@else
@php ($color = $app_color)
@endif
The color: {{$color}} //Output: The color: green
and if I included that blade to another blade its not set this var. like:
index.blade.php
@include('header')
The Color: {{$color}}
//Output: The Color: green
//The Color: null
Upvotes: 3
Views: 1327
Reputation: 1981
Because $color is the local variable. It is same as if you declare $color variable in A function and it's scope is within the function. If you can access $color in B function of the same class it's not accessible. Your above situation same like this.
You can put this code in your controller:
if(isset($company)) {
$color = $company->color;
} else {
$color = $app_color;
}
return view('index', compact('color', $color));
And Now you can access $color variable in both index and header view if you include this line @include('header') in index blade view. I hope this will be helpful.
Upvotes: 0
Reputation: 111
Please try this
header.blade.php
@if(isset($company))
@php $color = $company->color; @endphp
@else
@php $color = $app_color; @endphp
@endif
index.blade.php
@include('header')
The Color: {{ $color}}
Upvotes: 2