php - 如何在 Laravel 6 刀片视图中的请求中获取值是否在数组内部?
问题描述
我有一个表单,您可以在其中检查过滤器,稍后将过滤搜索查询。该表单正在工作,但我希望在请求后将选中的复选框标记为选中,以便用户可以看到他选择的内容。
它与一个复选框一起工作,但如果我添加一个数组,它就不再工作了。
<input type="text" class="form-control bg-transparent border-0 searchbox" placeholder="{{ __('general.search_placeholder') }}" value="{{ app('request')->input('q') }}" name="q" aria-label="Search" aria-describedby="button-addon2">
<div class="input-group-append">
<button class="btn border-0 searchbox" type="submit " id="button-addon2"><i class="fas fa-search"></i></button>
</div>
@foreach($collections as $collection)
<div class="form-check">
<input class="form-check-input" type="checkbox" value="{{$collection->id}}" name="s[]" id="defaultCheck{{$collection->id}}" {{ app('request')->input('s') == $collection->id ? 'checked' : '' }}>
<label class="form-check-label" for="defaultCheck{{$collection->id}}">
{{$collection->id}}
</label>
</div>
@endforeach
</form>```
Does anybody know how to solve that?
解决方案
如果s[]
是一组选中的复选框,您可以in_array
用来测试当前$collection->id
是否在其中:
{{ in_array($collection->id, app('request')->input('s')) ? 'checked' : '' }}
推荐阅读
- python - 将插槽连接到来自类派生的所有对象的信号
- python - python REGEX - 如何从字符串中删除方括号?
- node.js - React Apollo - nodejs graphql后端返回异步响应后如何重新获取(更新)数据
- javascript - Javascript 从 cognos 10 更改为 cognos 11.1.7
- java - 带有 ScheduledExecutorService 的 java 中的重试机制
- prometheus - PromQL 查询中的动态阈值
- python - NameError: name 'tk' is not defined repl.it
- database - Zsh:找不到命令:mongo 尝试使用 brew 安装 mongodb 4.2 后
- python - 如何在 django rest 框架 API 中显示 json 格式的字段?
- python - 将 pandas DataFrame 中的一行替换为基于唯一列值的 dict 项