首页 > 解决方案 > 如何在 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?

标签: phplaraveleloquent

解决方案


如果s[]是一组选中的复选框,您可以in_array用来测试当前$collection->id是否在其中:

{{ in_array($collection->id, app('request')->input('s')) ? 'checked' : '' }}

推荐阅读