php - 如何修复 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”。
解决方案
您可以在循环之前检查该变量
@if(!empty($usr_color))
// you can loop here
@endif
推荐阅读
- android - Getting class cache errors when building with Gradle
- terraform - 如何在 terraform 中定义全局变量?
- powershell - 如何格式化多个 Pssession 结果的输出以在每个计算机名之间包含一个空格
- python - Python 请求相对于脚本目录的目录
- javascript - 复选框未选中 onclick JavaScript
- c# - 需要将左连接 SQL 转换为 linq 查询 - 感谢帮助
- swift - 为自定义的 TabBar 添加阴影
- java - 每个新动画总是使用 AnimationDrawable 运行一个函数
- aws-lambda - 从 DynamoDB 流复制到 KinesisData 流时如何保持顺序并避免重复记录?
- visual-studio - 从我的 Azure DevOps Artifacts 源安装上游 NuGet 包(以将它们保存在该源中)没有效果