我有 2 种情况,但最简单的情况如下: 数据库列填充有表示时间的字符串(例如 4:30 的课程)。当用户添加新条目时,用户会从下拉列表中选择时间。在编辑时,下拉数据从控制器以及要编辑的数据库行($classtime 和扩展 $classtime->start_time 包含用户选择的原始值...04:30)传递。相关代码是:
$classtime = Classtime::find($id); /* get the db row to be edited */
$data = array(
'clubs' => Club::all(),
'start_times' => [
'09:00','09:30','10:00','10:30','11:00','11:30','12:00','12:30','01:00','01:30','02:00','02:30','03:00','03:30','04:00','04:30','05:00','05:30','06:00','06:30','07:00','07:30','08:00','08:30',
],);
添加一个新条目将(例如)04:30 存储为“start_time”字段中的字符串。当需要编辑条目时,我希望下拉列表将 04:30 显示为所选值。在视图中构建列表时,我有以下内容:
@foreach($data['start_times'] as $stime)
<option value="{{ $stime }}">{{ $stime }}</option>
@endforeach
我也有 $classtime 作为数据库记录。
我的问题是:如何将 $stime(数组元素)与数据库行中的当前值 ($classtime->start_time) 进行比较,并基于相等的比较,将 'selected' 插入到选项中?我本以为这是一个常见问题,但我找不到任何与此相符的内容,也无法从我找到的回复中得出解决方案。任何帮助,将不胜感激。干杯杰夫
你可以这样做:
@foreach($data['start_times'] as $stime)
<option value="{{ $stime }}" @if(App\Classtime::latest()->first()->start_time === $stime) selected @endif>{{ $stime }}</option>
@endforeach
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句