我试图在 blade 中设置一个 php 变量。它可以在同一个 Blade 上工作,而不是在其他 Blade 上设置 var。
例子:
header.blade.php
@if(isset($company))
@php ($color = $company->color)
@else
@php ($color = $app_color)
@endif
The color: {{$color}} //Output: The color: green
如果我将该 Blade 包含到另一个 Blade 中,则不会设置此变量。 喜欢:
索引.blade.php
@include('header')
The Color: {{$color}}
//Output: The Color: green
//The Color: null
最佳答案
请试试这个
header.blade.php
@if(isset($company))
@php $color = $company->color; @endphp
@else
@php $color = $app_color; @endphp
@endif
索引.blade.php
@include('header')
The Color: {{ $color}}
关于php - 从 laravel 中包含的 Blade 获取 Blade 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53666949/