laravel - laravel blade syntax for loop how to call iteration index inside laravel helper functions
问题描述
I have the following for loop
@for($i = 0; $i < $contactAmount; $i++)
<div class="form-group row">
<label for="firstname" class="col-md-4 col-form-label text-md-right">voornaam</label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control @error('firstname.0') is-invalid @enderror" name="firstname[]" value="{{ old('firstname.0') }}" autofocus>
@error('firstname.0')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
@endfor
but if I replace the hard coded '0' with {{ $i }} my validation does not work anymore. So how do I call my integer and use it inside laravel helper functions?
@for($i = 0; $i < $contactAmount; $i++)
<div class="form-group row">
<label for="firstname" class="col-md-4 col-form-label text-md-right">voornaam</label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control @error('firstname.{{ $i }}') is-invalid @enderror" name="firstname[]" value="{{ old('firstname.'. $i) }}" autofocus>
@error('firstname.{{ $i }}')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
@endfor
解决方案
你为什么不使用一个@foreach
?您可以按如下方式访问循环的索引:
@foreach($contactAmount as $amount)
<div class="form-group row">
{{ $loop->iteration }}
...
</div>
@endforeach
推荐阅读
- delphi - 无法转换 XML“样式表不包含文档元素”(在任何地方都可以使用,但 delphi)
- windows - 我的盒子上的 Google SSL 证书有一个奇怪的问题
- visual-studio - 进行了无法编译的编辑。在 VS2019 中使用 Resharper 时
- python - REGEX 一起工作,由 | 分隔 或者。独立运行时都返回空列表
- json - 批处理 - Odata
- verilog - 使用 verilog 进行 Mips 模拟,lw 指令无法正常工作
- javascript - 如何为“扩展”提供动态值?(或“在这种情况下扩展如何工作”?)
- r - 如何在 RStudio Server 中安装 R 包
- javascript - 在 useEffect 挂钩中从窗口中删除滚动事件侦听器时遇到一些问题
- react-native - 使用 Visual Studio App Center 为 iOS 或 Android 构建 React Native 时,如何使用 Build Number 作为 App Version number 的一部分?