Laravel Blade PHP 表达式返回未定义的变量

问题描述 投票:0回答:1

我的控制器中有类似的东西传递给刀片

$fields = [
            'productCode' => [
                'type' => 'text',
                'validation' => 'required|min:4|max:10',
                'label' => 'Product Code',
            ],
    'productLine' => [
                'type' => 'select',
                'options' => ['Motorcycles' => 'Motorcycles', 'Classic Cars' => 'Classic Cars', 'Trucks and Buses' => 'Trucks and Buses'],
                'validation' => 'required',
                'label' => 'Product Line',
            ],

在我看来刀片

<?php 
        foreach ($fields as $field => $param): 
            $options = [];
            if (!empty($param['options'])):
                $options = $param['options'];
            endif;
        ?>
            <x-larastrap::{{ $param['type'] }} 
                name="{{ $field }}" 
                label="{{ $param['label'] }}"
                <?php if (!empty($options)): ?>
                :options="$options"
                <?php endif; ?>
            />
        <?php endforeach ?>

我得到了未定义的变量 $options 但 $options 值正在设置(我可以 print_r 并查看这些值。我怀疑这与 Blade 处理 php 表达式的方式有关。我似乎无法弄清楚为什么

php laravel laravel-blade
1个回答
0
投票

使用 json_encode 将 $options 变量编码为 JSON 字符串。然后,在 Blade 模板中,您可以将此 JSON 字符串作为选项属性传递,该属性将根据需要进行解码和使用。 @php $选项= json_encode($选项); // 将选项转换为 JSON 字符串以传递给 Blade 模板 @endphp

© www.soinside.com 2019 - 2024. All rights reserved.