首页 > 解决方案 > 如何修复 Laravel 中未定义的变量错误?

问题描述

我正在尝试运行我的编码,但我得到了未定义变量的错误。

这是我的控制器:

$usr_color = DB::table('listbox')
                ->select('lstbx_codename','lstbx_descname')
                ->where('lstbx_cat', '=', 'usr_prf_color')
                ->orderBy('lstbx_no', 'asc')
                ->get();

这是我的看法:

                                        <div class="form-group">
                                            <label>Color</label>
                                            <select name="usr_prf_color" class="form-control">
                                                @foreach ($usr_color as $color) 
                                                    <option value="{{ $color-> lstbx_codename }}" 
                                                        {{ old('usr_prf_color') == $color-> lstbx_codename ? 'selected' : '' }}>
                                                        {{ $color-> lstbx_descname }}
                                                    </option>
                                                @endforeach
    
                                                
                                                --}}
    
                                             </select>
                                        </div> 

我的错误是“未定义的变量:usr_color”。

标签: phplaravel

解决方案


您可以在循环之前检查该变量

@if(!empty($usr_color))

   // you can loop here

@endif

推荐阅读