gpt4 book ai didi

php - 从 laravel 中包含的 Blade 获取 Blade 中的变量

转载 作者:搜寻专家 更新时间:2023-10-31 20:57:39 29 4
gpt4 key购买 nike

我试图在 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com