html - 如何在控制器上的 html 标记中设置 if else 条件
问题描述
大家好,我想在我的控制器上使用 html 标签中的 foreach 设置我的选项
这是我的控制器:
$category = Subcategory::where('category_code', $request->category_code)->get();
$selected = 'selected';
foreach ($category as $item) {
$html .= '<option value="'.$city->code.'" '.if($request->subcategory_code==$city->code) { echo $selected; }.' >'.$city->name.'</option>';
}
解决方案
您可以使用三元运算符:
$category = Subcategory::where('category_code', $request->category_code)->get();
foreach ($category as $item) {
$html .= '<option value="' . $city->code . '"' . ($request->subcategory_code==$city->code ? ' selected' : '') . '>'.$city->name.'</option>';
}
推荐阅读
- python - 寻找一种在python中将2个列表重新链接在一起的方法
- php - Mysql:如何在 Mysql 查询列名中使用变量
- deep-learning - docker:无效的参考格式。请参阅“搬运工运行--帮助”
- python - ValueError:形状必须为 2 级,但“MatMul”为 1 级
- python - os.remove() {Python 3} 会永久删除指定的文件还是我的硬盘中留下的残留文件?
- arrays - nodejs有没有像java中的列表(集合)
- javascript - Apple 身份验证和 GDPR 矛盾
- python - 为什么 for 循环会这样?
- swift - SwiftUI - 动态文本的本地化
- javascript - 如何正则表达式用下划线替换匹配文本中的所有空格?(Javascript)